Search This Blog

Tuesday, April 12, 2011

C# get the actual executable directory (not the current working directory); C# get any source class directory;

Getting the actual executable directory comes handy when you need to execute the program remotely (e.g. from STAF). The execution directory is then different than the actual executable (*.exe) location. With the tricks below you need to create a *.log file near the (*.exe) file:

 

Console.WriteLine(Application.StartupPath);
Console.WriteLine(Path.GetDirectoryName(Application.ExecutablePath));
Console.WriteLine(Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(typeof(Program)).CodeBase));
Console.WriteLine(System.Reflection.Assembly.GetExecutingAssembly().Location);
Console.WriteLine(System.IO.Path.GetDirectoryName(Application.ExecutablePath));

These all return the actual executable directory path.

5 comments:

If you like this post, please leave a comment :)