ProcessInfo and RedirectStandardOutput

前端 未结 6 1142
無奈伤痛
無奈伤痛 2020-11-27 16:54

I have an app which calls another process in a command window and that process has updating stats that output to the console window. I thought this was a fairly simple opera

6条回答
  •  执笔经年
    2020-11-27 17:27

    Using lambda expressions, etc:

    var info = new ProcessStartInfo(path)
    {
        RedirectStandardError = true,
        RedirectStandardOutput = true,
        UseShellExecute = false,
        Verb = "runas",
    };
    
    var process = new Process
    {
        EnableRaisingEvents = true,
        StartInfo = info
    };
    
    Action actionWrite = (sender, e) =>
    {
        Console.WriteLine(e.Data);
    };
    
    process.ErrorDataReceived += (sender, e) => actionWrite(sender, e);
    process.OutputDataReceived += (sender, e) => actionWrite(sender, e);
    
    process.Start();
    process.BeginOutputReadLine();
    process.BeginErrorReadLine();
    process.WaitForExit();
    

提交回复
热议问题