我该如何获取我的.exe文件路径?因为如果我复制我的.exe文件,我就可以得到新的路径?
System.Reflection.Assembly.GetEntryAssembly().Location;
此外:
AppDomain.CurrentDomain.BaseDirectory
Assembly.GetEntryAssembly().Location
第一个是您应用程序可执行文件的目录。注意!它可以在运行时更改。
第二个是您从中运行代码的程序集(.dll)的目录。
Assembly.GetEntryAssembly().Location
对我来说有效。第一个有file;\
,而第二个提供了执行.exe文件的绝对路径。添加System.IO.Path.GetDirectoryName()
可仅获取路径。 - CraftedGamingstring exePath = Application.ExecutablePath;
string appPath = Application.StartupPath;
在Visual Studio 2008中,您可以使用以下代码:
var _assembly = System.Reflection.Assembly
.GetExecutingAssembly().GetName().CodeBase;
var _path = System.IO.Path.GetDirectoryName(_assembly) ;
System.IO.Path.GetDirectoryName
中也会出现 ArgumentException。 - benderto
AppDomain.CurrentDomain.BaseDirectory
。 - user799821Path.GetDirectoryName
即可获取不包含文件名的文件夹路径。 - ProfKSystem.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
- SnookerC