如何在控制台应用程序中找到应用程序的路径?
在Windows Forms中,我可以使用Application.StartupPath
查找当前路径,但这似乎在控制台应用程序中不可用.
如何在控制台应用程序中找到应用程序的路径?
在Windows Forms中,我可以使用Application.StartupPath
查找当前路径,但这似乎在控制台应用程序中不可用.
System.Reflection.Assembly.GetExecutingAssembly()
.Location
1
如果你想要的只是目录,就把它和System.IO.Path.GetDirectoryName
结合起来.
1As 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
will return the 'permanent' path of the assembly.