开发者

start process issue

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 s开发者_如何学JAVAome sample codes.


using (var process = Process.Start("test.exe"))
{
    process.WaitForExit();
    var exitCode = process.ExitCode;
}


    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;
        }
    }


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜