获取我的.exe路径

kar*_*tal 179 c#

我如何获得我的.exe路径,因为如果我复制我的.exe,我可以得到我的新路径?

Gab*_*ams 231

System.Reflection.Assembly.GetEntryAssembly().Location;
Run Code Online (Sandbox Code Playgroud)

  • 我只想要Path但我看到这个命令给了我exe的路径+文件名.:-(另一方面GetEntryAssembly().位置给出了"file://"的路径 - 我需要的是`AppDomain.CurrentDomain.BaseDirectory` (119认同)
  • 这在单元测试项目中不起作用.GetEntryAssembly()为null. (18认同)
  • System.Reflection.Assembly.GetExecutingAssembly().Location返回执行程序集当前所在的位置,可能是也可能不是程序集在不执行时所在的位置.对于卷影复制程序集,您将获得临时目录中的路径.System.Reflection.Assembly.GetExecutingAssembly().CodeBase将返回程序集的"permenant"路径. (9认同)
  • @ProfK:谢谢,这正是我所需要的.完整的解决方案对我有用:`System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)` (6认同)
  • @ user799821只需使用`Path.GetDirectoryName`来获取没有文件名的文件夹. (4认同)

Ali*_*tad 114

此外:

AppDomain.CurrentDomain.BaseDirectory
Assembly.GetEntryAssembly().Location
Run Code Online (Sandbox Code Playgroud)

  • AppDomain.CurrentDomain.BaseDirectory在单元测试项目中工作. (23认同)
  • `Assembly.GetEntryAssembly().Location` 对我有用。第一个有`file;\`,而第二个提供正在执行的.exe 的绝对文件路径。添加`System.IO.Path.GetDirectoryName()`以仅获取路径 (2认同)

Ric*_*kir 63

在Windows窗体项目中:

对于完整路径(包含文件名):string exePath = Application.ExecutablePath;
仅适用于路径:string appPath = Application.StartupPath;

  • "如果在exe快捷方式中设置了Application.StartupPath将受"工作目录"的影响,或者该进程是从其他应用程序启动的." (6认同)