Monday, October 15, 2007

Executable context

Sometimes it is known that certain files (the .exe.config in particular) that are necessary for the running of an application reside in the same folder as the executable. Now, if the executable is not run from the directory where it reside it can run into problems locating these necessary files. In this case the following code comes to the rescue:

static void Main()
string directory = Path.GetDirectoryName(Application.ExecutablePath);
Process.GetCurrentProcess().StartInfo.WorkingDirectory = directory;
Application.Run(new Form1());

No comments: