Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the application's directory from a WPF application

Tags:

c#

.net

wpf

People also ask

How do I find my WPF application?

Open it with reflector and see whether it references one of the PresentationFramework DLLs (then it's likely WPF) or System. Windows. Forms. dll.

Is WPF still in demand?

WPF is still one of the most used app frameworks in use on Windows (right behind WinForms).

Where does the execution start in a WPF application?

For a WPF standalone application that is generated in Visual Studio using the New Project wizard, the entry point for the application is the Main function, defined in App. g. cs (generated code). In the default project, this is the public static void App.


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

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

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


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