How to get log from Process.Start

前端 未结 2 564
既然无缘
既然无缘 2020-12-19 11:33

I\'m going to precompile an asp.net application in my custom c# form. How do i retrieve the process logs and check whether it is a successful process or not?

Here\'s

2条回答
  •  佛祖请我去吃肉
    2020-12-19 11:41

    Set your ProcessStartInfo.RedirectStandardOutput to true - this will redirect all output to Process.StandardOutput, which is a stream that you can read to find all output messages:

    ProcessStartInfo process = new ProcessStartInfo 
    { 
       CreateNoWindow = false,
       UseShellExecute = false,
       WorkingDirectory = msPath,
       RedirectStandardOutput = true,
       FileName = msCompiler,
       Arguments = "-p {0} -v / {1}"
                .StrFormat(
                  CurrentSetting.CodeSource, 
                  CurrentSetting.CompileTarget)
    };
    
    Process p = Process.Start(process);
    string output = p.StandardOutput.ReadToEnd();
    

    You can also use the OutputDataReceived event in a similar way to what @Bharath K describes in his answer.

    There are similar properties/events for StandardError - you will need to set RedirectStandardError to true as well.

提交回复
热议问题