03 March 2008

Starting an external program in the same directory

Especially in development/demo scenario's you sometimes need to start an external program. In my case, it was a simple batch file that more or less terminated and restarted a Map Server service in a rather ungraceful way. But sometimes you want to go for simple. First, you have to find out in which directory your external program is. This is the same directory as where your program runs:
string assemblyBasePath =
then, you have to create the full path to your executable, for instance
string exec = Path.Combine(assemblyBasePath, "yourbatchfile.bat");
and finally, you have to launch the process:
ProcessStartInfo psi = new ProcessStartInfo(exec);
psi.WindowStyle = ProcessWindowStyle.Hidden;
Process proc = Process.Start(psi);
and that's all there is to it. The call to proc.WaitForExit() makes sure that your own program waits for the batch file "yourbatchfile.bat" to complete, while ProcessWindowStyle.Hidden prevents a console window popping up. If you do want to see the console window, for debugging purposes for example, use ProcessWindowStyle.Normal instead

No comments: