tags:

views:

50

answers:

3

Hello everyone,

I am using VSTS 2008 + C# + .Net 3.5 to develop a console application. And I want to start an external process (an exe file) from my C# application, and I want current C# application to be blocked until the external process stops and I also want to get the return code of the external process.

Any ideas how to implement this? Appreciate if some sample codes.

thanks in advance, George

+8  A: 
using (var process = Process.Start("test.exe"))
{
    process.WaitForExit();
    var exitCode = process.ExitCode;
}
Darin Dimitrov
Adding some exception handling code can make it better. :)
Lex Li
Also wrapping the `process` in a `using` block would help :)
GSerg
+1  A: 

you'll find all the needed documentation here: http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo(VS.80).aspx

PierrOz
+1  A: 
    public static String ShellExec( String pExeFN, String pParams, out int exit_code)
    {
        System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(pExeFN, pParams);
        psi.RedirectStandardOutput = true;
        psi.UseShellExecute = false; // the process is created directly from the executable file
        psi.CreateNoWindow = true;

        using (System.Diagnostics.Process p = System.Diagnostics.Process.Start(psi))
        {
            string tool_output = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            exit_code = p.ExitCode;

            return tool_output;
        }
    }
Nightingale7