如何在 net控制台应用程序中获取应用程序的路径?


如何在控制台应用程序中找到应用程序的路径?

在Windows窗体中,我可以使用Application.StartupPath来查找当前路径,但是在控制台应用程序中似乎没有这个功能。

System.Reflection.Assembly.GetExecutingAssembly().Location1

把这个和System.IO.Path.GetDirectoryName结合起来,如果你想要的就是目录。

1 As per Mr.Mindor’s comment:
System.Reflection.Assembly.GetExecutingAssembly().Location returns where
the executing assembly is currently located, which may or may not be where the
assembly is located when not executing. In the case of shadow copying
assemblies, you will get a path in a temp directory.
System.Reflection.Assembly.GetExecutingAssembly().CodeBase.aspx) will
return the ‘permanent’ path of the assembly.

您可以使用下面的代码来获取当前的应用程序目录。

AppDomain.CurrentDomain.BaseDirectory

未经作者同意,本文严禁转载,违者必究!