Getting the application's directory from a WPF application

Viewed 213941

I found solutions for Windows Forms with AppDomain but what would be the equivalent for a WPF Application object?

9 Answers

One method:

System.AppDomain.CurrentDomain.BaseDirectory

Another way to do it would be:

System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)

Here is another:

System.Reflection.Assembly.GetExecutingAssembly().Location
String exePath = System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName;
 string dir = Path.GetDirectoryName(exePath);

Try this!

Try this. Don't forget using System.Reflection.

string baseDir = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

You can also use freely Application.StartupPath from System.Windows.Forms, but you must to add reference for System.Windows.Forms assembly!

Just thought I'd add an updated answer for those who need it.

I used to use: My.Application.Info.DirectoryPath to get my applications path. It seems NET6 didn't like this. After using one of the examples below, I noticed IntelliSense suggested this: Environment.ProcessPath

Thus, to get the path to the application exe:

Environment.ProcessPath

To get the folder:

Path.GetDirectoryName(Environment.ProcessPath)

Hope this helps.

Related