如何找到我的控制台应用程序在c#中运行的目录?

时间:2022-03-19 08:51:29

How do I find out what directory my console app is running in with C#?

如何找到我的控制台应用程序在c#中运行的目录?

6 个解决方案

#1


131  

To get the directory where the .exe file is:

获取.exe文件所在目录:

AppDomain.CurrentDomain.BaseDirectory

To get the current directory:

获取当前目录:

Environment.CurrentDirectory

#2


11  

Depending on the rights granted to your application, whether shadow copying is in effect or not and other invocation and deployment options, different methods may work or yield different results so you will have to choose your weapon wisely. Having said that, all of the following will yield the same result for a fully-trusted console application that is executed locally at the machine where it resides:

根据授予您的应用程序的权限,影子复制是否有效以及其他调用和部署选项,不同的方法可以工作或产生不同的结果,因此您必须明智地选择您的武器。话虽如此,对于一个完全可信的控制台应用程序,以下所有内容都将产生相同的结果,该应用程序在其所在的机器上本地执行:

Console.WriteLine( Assembly.GetEntryAssembly().Location );
Console.WriteLine( new Uri(Assembly.GetEntryAssembly().CodeBase).LocalPath );
Console.WriteLine( Assembly.GetEntryAssembly().Location );
Console.WriteLine( Environment.GetCommandLineArgs()[0] );
Console.WriteLine( Process.GetCurrentProcess().MainModule.FileName );

You will need to consult the documentation of the above members to see the exact permissions needed.

您将需要查阅上述成员的文档,以查看所需的确切权限。

#3


5  

Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)

Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)

#4


3  

In .NET, you can use System.Environment.CurrentDirectory to get the directory from which the process was started.
System.Reflection.Assembly.GetExecutingAssembly().Location will tell you the location of the currently executing assembly (that's only interesting if the currently executing assembly is loaded from somewhere different than the location of the assembly where the process started).

在。net中,可以使用System.Environment。获取进程开始的目录的CurrentDirectory。System.Reflection.Assembly.GetExecutingAssembly()。Location将告诉您当前正在执行的程序集的位置(只有当当前正在执行的程序集从与进程开始的程序集的位置不同的地方加载时,才会有趣)。

#5


1  

On windows (not sure about Unix etc.) it is the first argument in commandline.

在windows上(不确定Unix等),这是命令行中的第一个参数。

In C/C++ firts item in argv*

C/ c++ firts项目,argv*

WinAPI - GetModuleFileName(NULL, char*, MAX_PATH)

WinAPI - GetModuleFileName(NULL, char*, MAX_PATH)

#6


1  

Application.StartUpPath;

Application.StartUpPath;

#1


131  

To get the directory where the .exe file is:

获取.exe文件所在目录:

AppDomain.CurrentDomain.BaseDirectory

To get the current directory:

获取当前目录:

Environment.CurrentDirectory

#2


11  

Depending on the rights granted to your application, whether shadow copying is in effect or not and other invocation and deployment options, different methods may work or yield different results so you will have to choose your weapon wisely. Having said that, all of the following will yield the same result for a fully-trusted console application that is executed locally at the machine where it resides:

根据授予您的应用程序的权限,影子复制是否有效以及其他调用和部署选项,不同的方法可以工作或产生不同的结果,因此您必须明智地选择您的武器。话虽如此,对于一个完全可信的控制台应用程序,以下所有内容都将产生相同的结果,该应用程序在其所在的机器上本地执行:

Console.WriteLine( Assembly.GetEntryAssembly().Location );
Console.WriteLine( new Uri(Assembly.GetEntryAssembly().CodeBase).LocalPath );
Console.WriteLine( Assembly.GetEntryAssembly().Location );
Console.WriteLine( Environment.GetCommandLineArgs()[0] );
Console.WriteLine( Process.GetCurrentProcess().MainModule.FileName );

You will need to consult the documentation of the above members to see the exact permissions needed.

您将需要查阅上述成员的文档,以查看所需的确切权限。

#3


5  

Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)

Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)

#4


3  

In .NET, you can use System.Environment.CurrentDirectory to get the directory from which the process was started.
System.Reflection.Assembly.GetExecutingAssembly().Location will tell you the location of the currently executing assembly (that's only interesting if the currently executing assembly is loaded from somewhere different than the location of the assembly where the process started).

在。net中,可以使用System.Environment。获取进程开始的目录的CurrentDirectory。System.Reflection.Assembly.GetExecutingAssembly()。Location将告诉您当前正在执行的程序集的位置(只有当当前正在执行的程序集从与进程开始的程序集的位置不同的地方加载时,才会有趣)。

#5


1  

On windows (not sure about Unix etc.) it is the first argument in commandline.

在windows上(不确定Unix等),这是命令行中的第一个参数。

In C/C++ firts item in argv*

C/ c++ firts项目,argv*

WinAPI - GetModuleFileName(NULL, char*, MAX_PATH)

WinAPI - GetModuleFileName(NULL, char*, MAX_PATH)

#6


1  

Application.StartUpPath;

Application.StartUpPath;