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.

4 comments:

  1. Thanks - a quick google search turned this up and it was what I was looking for.

    ReplyDelete
  2. Reference System.Windows.Forms for the Application object to be found. This is mostly for a Console application or a non-Windows.Forms program.

    ReplyDelete
  3. Thank you. Clear and short. Very useful.

    ReplyDelete

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