How to get the current executable's path in C#

Sometimes you may want to get the current executable (i.e. the executable your program is currently running in) path, This is needed to access some settings file, database file or resources file residing in the same directory as executable. 

There are several options to find the current executable path. But I found the one working both for console applications and for Windows Forms Applications:

string strExeFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string strWorkPath = System.IO.Path.GetDirectoryName(strExeFilePath); 


The variable strWorkPath will contain the current path of executable residing in. You can use it later to access your settings or asset files: 

string strSettingsXmlFilePath = System.IO.Path.Combine(strWorkPath, "Settings.xml");


Please be careful when storing the settings in the Program Files folder, This folder is protected by Windows security policy for non-administrative accounts. For user accounts the settings files are automatically redirected to respective folder inside AppData folder. So if the user runs the program twice, one time using administrator account, and second time using user account, he may get two separate settings files. This may lead to confusion. So, for storing the settings it is better to use dedicated folders. Further reading may be here


Professional custom Windows software development services from IQ Direct Inc., USA.