如何在控制台应用程序中找到应用程序的路径?
在Windows窗体中,我可以使用应用程序。StartupPath来查找当前路径,但这在控制台应用程序中似乎不可用。
如何在控制台应用程序中找到应用程序的路径?
在Windows窗体中,我可以使用应用程序。StartupPath来查找当前路径,但这在控制台应用程序中似乎不可用。
当前回答
技术和陷阱一直在变化。下面假设你在linux上运行。net 6控制台应用程序(在win/mac上,结果将遵循类似的模式,只需将/usr/share/和/home/username/替换为操作系统的标准位置)。
演示:
Console.WriteLine("Path.GetDirectoryName(Process.GetCurrentProcess()?.MainModule?.FileName) = " + Path.GetDirectoryName(Process.GetCurrentProcess()?.MainModule?.FileName));
Console.WriteLine("Path.GetDirectoryName(Environment.ProcessPath) = " + Path.GetDirectoryName(Environment.ProcessPath));
Console.WriteLine("Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) = " + Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
Console.WriteLine("typeof(SomeType).Assembly.Location = " + typeof(SomeType).Assembly.Location);
Console.WriteLine("Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) = " + Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]));
Console.WriteLine("AppDomain.CurrentDomain.BaseDirectory = " + AppDomain.CurrentDomain.BaseDirectory);
Console.WriteLine("System.AppContext.BaseDirectory = " + System.AppContext.BaseDirectory);
结果:
Path.GetDirectoryName(Process.GetCurrentProcess()?.MainModule?.FileName) = /usr/share/dotnet
Path.GetDirectoryName(Environment.ProcessPath) = /usr/share/dotnet
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) = /home/username/myproject/bin/Debug/net6.0
typeof(SomeType).Assembly.Location = /home/username/myproject/bin/Debug/net6.0
Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) = /home/username/myproject/bin/Debug/net6.0
AppDomain.CurrentDomain.BaseDirectory = /home/username/myproject/bin/Debug/net6.0/
System.AppContext.BaseDirectory = /home/username/myproject/bin/Debug/net6.0/
每种方法都有其优点和缺点-请参阅其他答案以了解在哪种情况下使用哪种方法。
我用dotnet myapp运行我的.NET 6控制台应用程序,所以对我来说(可靠的)工作是:
typeof(SomeType).Assembly.Location
// or
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
其他回答
在。net6中,我的WPF应用程序(<TargetFramework>net6.0-windows</TargetFramework>)返回Assembly.GetEntryAssembly()的.dll文件路径!位置,而不是。exe文件。他们引入了System.Environment.ProcessPath:
var path = Environment.ProcessPath; // Note it may be null
返回启动当前执行进程的可执行文件的路径。当路径不可用时返回null。
请参见这里和这里的讨论。
.Location1 System.Reflection.Assembly.GetExecutingAssembly ()
如果你想要的只是目录,将它与System.IO.Path.GetDirectoryName结合起来。
根据minor先生的评论: System.Reflection.Assembly.GetExecutingAssembly()。Location返回正在执行的程序集当前所在的位置,该位置可能是也可能不是程序集未执行时所在的位置。在阴影复制程序集的情况下,您将获得临时目录中的路径。System.Reflection.Assembly.GetExecutingAssembly()。CodeBase将返回程序集的“永久”路径。
在VB.net
My.Application.Info.DirectoryPath
工作为我(应用类型:类库)。不太懂c#… 返回文件名为字符串的路径
Assembly.GetEntryAssembly()。Location或Assembly.GetExecutingAssembly().Location
与System.IO.Path.GetDirectoryName()结合使用只获取目录。
来自GetEntryAssembly()和GetExecutingAssembly()的路径可以不同,即使在大多数情况下目录是相同的。
使用GetEntryAssembly(),你必须意识到,如果入口模块是非托管的(即c++或VB6可执行文件),它可能返回null。在这些情况下,可以使用Win32 API中的GetModuleFileName:
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetModuleFileName(HandleRef hModule, StringBuilder buffer, int length);
上面的答案是我需要的90%,但返回了一个Uri,而不是常规路径。
正如在MSDN论坛的帖子中解释的那样,如何将URI路径转换为正常的文件路径?,我使用了以下方法:
// Get normal filepath of this assembly's permanent directory
var path = new Uri(
System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
).LocalPath;