'Getting the application's directory from a WPF application

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



Solution 1:[1]

One method:

System.AppDomain.CurrentDomain.BaseDirectory

Another way to do it would be:

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

Solution 2:[2]

Here is another:

System.Reflection.Assembly.GetExecutingAssembly().Location

Solution 3:[3]

You can also use the first argument of the command line arguments:

String exePath = System.Environment.GetCommandLineArgs()[0]

Solution 4:[4]

I used simply string baseDir = Environment.CurrentDirectory; and its work for me.

Good Luck

Edit:

I used to delete this type of mistake but i prefer to edit it because i think the minus point on this answer help people to know about wrong way. :) I understood the above solution is not useful and i changed it to string appBaseDir = System.AppDomain.CurrentDomain.BaseDirectory; Other ways to get it are:

1. string baseDir =   
    System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
 2. String exePath = System.Environment.GetCommandLineArgs()[0];
 3. string appBaseDir =    System.IO.Path.GetDirectoryName
    (System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);

Good Luck

Solution 5:[5]

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

Try this!

Solution 6:[6]

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

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

Solution 7:[7]

I tried this:

    label1.Content = Directory.GetCurrentDirectory();

and get also the directory.

Solution 8:[8]

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

Solution 9:[9]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Doug
Solution 2 Juan Manuel Serrat
Solution 3 Dr. Rajesh Rolen
Solution 4
Solution 5 Arsen Mkrtchyan
Solution 6
Solution 7 paul
Solution 8 crash
Solution 9 video.baba