Starting an external program in the same directory
less than 1 minute read
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 =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
then, you have to create the full path to your executable, for instancestring 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);
proc.WaitForExit();
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