Process.Start() get errors from command prompt window

前端 未结 3 802
梦如初夏
梦如初夏 2020-12-19 00:35

I\'m trying to Start command promt process with args. Now I want to obtain information about errors if they exist.

someProcess = System.Diag         


        
相关标签:
3条回答
  • 2020-12-19 00:52

    Process.StandardError Property:

    Gets a stream used to read the error output of the application.

    This should do what you want.

    Note

    To use StandardError, you must set ProcessStartInfo.UseShellExecute to false, and you must set ProcessStartInfo.RedirectStandardError to true. Otherwise, reading from the StandardError stream throws an exception.

    0 讨论(0)
  • 2020-12-19 01:05

    The other answers are correct. Here is some code you could use:

    ProcessStartInfo startInfo = new ProcessStartInfo(cmd, args);
    startInfo.UseShellExecute = false;
    startInfo.RedirectStandardError = true;
    Process someProcess = Process.Start(startInfo);
    string errors = someProcess.StandardError.ReadToEnd();
    

    Note that if the file is not found you won't get an error on standard error. You will get an exception instead.

    0 讨论(0)
  • 2020-12-19 01:14

    You're probably looking for the StandardError property.

    0 讨论(0)
提交回复
热议问题