Getting the application's directory from a WPF application
C#.NetWpfC# Problem Overview
I found solutions for Windows Forms with AppDomain but what would be the equivalent for a WPF Application
object?
C# Solutions
Solution 1 - C#
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 - C#
Here is another:
System.Reflection.Assembly.GetExecutingAssembly().Location
Solution 3 - C#
You can also use the first argument of the command line arguments:
String exePath = System.Environment.GetCommandLineArgs()[0]
Solution 4 - C#
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 - C#
String exePath = System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName;
string dir = Path.GetDirectoryName(exePath);
Try this!
Solution 6 - C#
Try this. Don't forget using System.Reflection
.
string baseDir = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Solution 7 - C#
I tried this:
label1.Content = Directory.GetCurrentDirectory();
and get also the directory.
Solution 8 - C#
You can also use freely Application.StartupPath from System.Windows.Forms, but you must to add reference for System.Windows.Forms assembly!
Solution 9 - C#
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.