如何使用 C# 找出我的控制台应用程序在哪个目录中运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/97312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I find out what directory my console app is running in with C#?
提问by John Sheehan
How do I find out what directory my console app is running in with C#?
如何使用 C# 找出我的控制台应用程序在哪个目录中运行?
采纳答案by Hallgrim
To get the directory where the .exe file is:
要获取 .exe 文件所在的目录:
AppDomain.CurrentDomain.BaseDirectory
To get the current directory:
获取当前目录:
Environment.CurrentDirectory
回答by Jakub Kotrla
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++ 中,argv* 中的第一个项
WinAPI - GetModuleFileName(NULL, char*, MAX_PATH)
WinAPI - GetModuleFileName(NULL, char*, MAX_PATH)
回答by Travis Illig
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
将告诉您当前正在执行的程序集的位置(只有当当前正在执行的程序集是从不同于进程开始的程序集位置的某个位置加载时才有意义)。
回答by Atif Aziz
Depending on the rights granted to your application, whether shadow copyingis 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.
您需要查阅上述成员的文档以查看所需的确切权限。
回答by Atif Aziz
Application.StartUpPath;
应用程序启动路径;
回答by Jason Kanaris
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
回答by R M Shahidul Islam Shahed
Let's say your .Net core console application project name is DataPrep.
假设您的 .Net 核心控制台应用程序项目名称是 DataPrep。
Get Project Base Directory:
获取项目基础目录:
Console.WriteLine(Environment.CurrentDirectory);
Output:~DataPrep\bin\Debug\netcoreapp2.2
输出:~DataPrep\bin\Debug\netcoreapp2.2
Get Project .csproj file directory:
string ProjectDirPath = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, @"..\..\..\"));
Console.WriteLine(ProjectDirPath);
Output:~DataPrep\
输出:~DataPrep\