In WPF, how can I get the path to my executable?


Overview

In a C# WPF application, you can get the full path to the startup executable using the System.Diagnostics.Process class:

string exePath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;

This code gets the current process using the GetCurrentProcess() method, and then gets the MainModule of the process. Finally, it gets the file actual executable name using the FileName property, which returns the full path to the executable file.

Note that this code should be executed after the InitializeComponent() method call in the MainWindow constructor, or in the Loaded event handler of the MainWindow. If GetCurrentProcess() is called too early in the application life cycle it may return a null value.