WPF 框架 3.5 上的 C# Application.ExecutablePath

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17966871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:19:29  来源:igfitidea点击:

C# Application.ExecutablePath on WPF Framework 3.5

c#wpfpathexecutable

提问by Fred Smith

This line is fine in a WinForm Framework3.5 but not in a WPF Framework3.5.

这一行在 WinForm Framework3.5 中很好,但在 WPF Framework3.5 中则不然。

Path.GetDirectoryName(Application.ExecutablePath); 

How can I get the exe path on a WPF app ?

如何获取 WPF 应用程序上的 exe 路径?

回答by Dzmitry Martavoi

There are several ways to get exe path. Try the next:

获取exe路径的方法有多种。尝试下一个:

  • Application.StartupPath
  • Path.GetDirectoryName(Environment.GetCommandLineArgs()[0])
  • Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)
  • Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)
  • System.Reflection.Assembly.GetEntryAssembly().Location
  • 应用程序启动路径
  • Path.GetDirectoryName(Environment.GetCommandLineArgs()[0])
  • Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)
  • Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)
  • System.Reflection.Assembly.GetEntryAssembly().Location

回答by Ned Stoyanov

Try this:

尝试这个:

System.IO.Path.GetDirectoryName( 
  System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );

回答by Joe Sonderegger

You can unpack a Uri like this:

您可以像这样解压 Uri:

string codeBase = Assembly.GetExecutingAssembly().CodeBase;
   UriBuilder uri = new UriBuilder(codeBase);
   string path = Uri.UnescapeDataString(uri.Path);