如何在控制台应用程序中找到应用程序的路径?
在 Windows Forms 中,我可以使用 Application.StartupPath
找到当前路径,但在控制台应用程序中似乎不可用。
如何在控制台应用程序中找到应用程序的路径?
在 Windows Forms 中,我可以使用 Application.StartupPath
找到当前路径,但在控制台应用程序中似乎不可用。
System.Reflection.Assembly.GetExecutingAssembly()
.Location
1
如果您只需要目录,请与System.IO.Path.GetDirectoryName
结合使用。
1根据Mindor先生的评论:
System.Reflection.Assembly.GetExecutingAssembly().Location
返回当前执行程序集所在的位置,这可能与未执行时程序集所在的位置相同也可能不同。在进行阴影复制程序集的情况下,您将获得临时目录中的路径。System.Reflection.Assembly.GetExecutingAssembly().CodeBase
将返回程序集的“永久”路径。
GetExecutingAssembly
返回包含当前正在执行的代码的程序集。这可能不一定是控制台.exe程序集,它可能是从完全不同的位置加载的程序集。你需要使用GetEntryAssembly
!此外请注意,在程序集位于GAC中时,可能未设置CodeBase
。更好的选择是AppDomain.CurrentDomain.BaseDirectory
。 - bitbonkAppDomain.CurrentDomain.BaseDirectory
BaseDirectory
可以在运行时设置?它只有一个getter(获取器)。 - bitbonk// To get the location the assembly is executing from
// (not necessarily where it normally resides on disk).
// In the case of using shadow copies, for instance in NUnit tests,
// this will be in a temp directory.
string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
// To get the location the assembly normally resides on disk or the install directory
string path = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
// Once you have the path you get the directory with:
var directory = System.IO.Path.GetDirectoryName(path);
var localDirectory = new Uri(directory).LocalPath;
。该语句将目录路径转换为本地文件系统路径,以便在不支持URI格式的情况下执行操作。请注意,该语句不会改变原始意思。 - Scott Solmer可能有点晚,但值得一提:
Environment.GetCommandLineArgs()[0];
更准确地说,要获取只有目录路径:
System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
编辑:
许多人指出,GetCommandLineArgs
不能保证返回程序名称。请参见命令行上的第一个单词只是传统意义上的程序名称。该文章确实指出:“虽然极少数的Windows程序使用这种怪癖(我自己不知道有哪些)”。因此,“假冒”GetCommandLineArgs
是可能的,但我们正在谈论控制台应用程序。控制台应用程序通常是快速且粗略的。所以这符合我的KISS理念。
编辑: 从反馈中看,大多数其他解决方案在使用单元测试系统时无法正常工作。这也是有道理的,因为可执行项不是您的应用程序,而是测试系统。我没有验证过这一点 - 所以我可能完全错了。如果是这样,我会删除这个编辑。
对于任何对asp.net web应用程序感兴趣的人,这里是我对3种不同方法的结果。
protected void Application_Start(object sender, EventArgs e)
{
string p1 = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string p2 = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
string p3 = this.Server.MapPath("");
Console.WriteLine("p1 = " + p1);
Console.WriteLine("p2 = " + p2);
Console.WriteLine("p3 = " + p3);
}
结果
p1 = C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\a897dd66\ec73ff95\assembly\dl3\ff65202d\29daade3_5e84cc01
p2 = C:\inetpub\SBSPortal_staging\
p3 = C:\inetpub\SBSPortal_staging
该应用程序实际上正在从"C:\inetpub\SBSPortal_staging"运行,因此第一种解决方案绝对不适用于Web应用程序。
// Get normal filepath of this assembly's permanent directory
var path = new Uri(
System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
).LocalPath;
File.CreateDirectory(path)
的操作,否则它会抛出异常,因为它不允许URI路径... - vapcguy#
字符)的路径无效。标识符及其后面的所有内容都将从结果路径中截断。 - bgfvdu3wnew Uri
和 System.IO.Path.GetDirectoryName
?这样可以得到一个普通的路径字符串,而不是一个 Uri
。 - Timo如果你正在寻找一个与 .NET Core 兼容的方式,请使用
System.AppContext.BaseDirectory
这是在.NET Framework 4.6和.NET Core 1.0(以及.NET Standard 1.3)中引入的。请参见:AppContext.BaseDirectory Property。
根据此页面,
这是.NET Core中AppDomain.CurrentDomain.BaseDirectory的首选替代方法。
Process.GetCurrentProcess().MainModule.FileName
。 - Gavin您可能正在寻找以下内容:
System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
你可以使用这一个代替。
System.Environment.CurrentDirectory
对于控制台应用程序,您可以尝试以下方法:
System.IO.Directory.GetCurrentDirectory();
输出结果(在我的本地电脑上):
c:\users\xxxxxxx\documents\visual studio 2012\Projects\ImageHandler\GetDir\bin\Debug
或者您可以尝试(末尾有一个额外的反斜杠):
AppDomain.CurrentDomain.BaseDirectory
输出:
c:\users\xxxxxxx\documents\visual studio 2012\Projects\ImageHandler\GetDir\bin\Debug\
BaseDirectory
可以在运行时设置。但不能保证其正确性。 - Yousha Aleayoub