С# Application.ExecutablePath в WPF Framework 3.5

Эта строка подходит для WinForm Framework3.5, но не для WPF Framework3.5.

Path.GetDirectoryName(Application.ExecutablePath); 

Как я могу получить путь к exe в приложении WPF?


person Fred Smith    schedule 31.07.2013    source источник


Ответы (3)


Есть несколько способов получить путь к exe. Попробуйте следующее:

  • Application.StartupPath
  • Path.GetDirectoryName(Environment.GetCommandLineArgs()[0])
  • Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)
  • Path.GetDirectoryName(Assembly.GetEntryAssembly().Расположение)
  • System.Reflection.Assembly.GetEntryAssembly().Расположение
person Dzmitry Martavoi    schedule 31.07.2013
comment
Assembly.Location в некоторых случаях может вернуть null, лучше использовать AssemblyName.CodeBase, пример ниже от Неда Стоянова - person ili; 10.04.2014
comment
Процесс IDisposable, что означает, что вы должны обернуть третий вариант блоком using - person RE6; 21.04.2014

Попробуй это:

System.IO.Path.GetDirectoryName( 
  System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
person NeddySpaghetti    schedule 31.07.2013
comment
System.IO.Path.GetDirectoryName( new Uri( System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath ); - это правильно, CodeBase возвращает URL, а Path.GetDirectoryName(...) не работает с URL - person ili; 10.04.2014

Вы можете распаковать Uri следующим образом:

string codeBase = Assembly.GetExecutingAssembly().CodeBase;
   UriBuilder uri = new UriBuilder(codeBase);
   string path = Uri.UnescapeDataString(uri.Path);
person Joe Sonderegger    schedule 19.01.2015