4

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

Path.GetDirectoryName(Application.ExecutablePath); 

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

Imad Alazani
  • 6,688
  • 7
  • 36
  • 58
Fred Smith
  • 2,047
  • 3
  • 25
  • 36

3 Answers3

12

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

  • 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
demidov-alex
  • 142
  • 1
  • 1
  • 9
Dzmitry Martavoi
  • 6,867
  • 6
  • 38
  • 59
  • 1
    `Assembly.Location` can return `null` in some cases, it is better to use `AssemblyName.CodeBase`, the example is below by Ned Stoyanov – ili Apr 10 '14 at 06:14
  • Process is IDisposable, which means that you have to wrap the third option with a `using` block – RE6 Apr 21 '14 at 12:40
2

Try this:

System.IO.Path.GetDirectoryName( 
  System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
NeddySpaghetti
  • 13,187
  • 5
  • 32
  • 61
  • 1
    `System.IO.Path.GetDirectoryName( new Uri( System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath );` - this is correct, `CodeBase` returns URL, and `Path.GetDirectoryName(...)` doesn't work with URLs – ili Apr 10 '14 at 06:10
1

You can unpack a Uri like this:

string codeBase = Assembly.GetExecutingAssembly().CodeBase;
   UriBuilder uri = new UriBuilder(codeBase);
   string path = Uri.UnescapeDataString(uri.Path);
Joe Sonderegger
  • 784
  • 5
  • 15