In this article, a new program is started with System. Diagnosis. Process to read the output from its standard output stream, and then the TextBox of the output display interface is displayed. First, let's look at several related attributes and methods provided by the Process class.
1. bool RedirectStandardOutput
Indicates whether to redirect standard output. If you want to read the output from the standard output stream, this attribute must be set to True. (Framework 1.1 does not have this attribute ).
2. bool UseShellExecute
Indicates whether to start from the console. If you want to read the output from the standard output stream, this attribute must be set to False. (Framework 1.1 does not have this attribute ).
3. event OutputDataReceived
This is an event. If you specify a response function for it, the response function can obtain the output data. (This event is not found in Framework 1.1 ).
4. StreamReader StandardOutput
Standard output stream.
The following two methods are used to read the output from the standard output stream. The first method is applicable to the. NET Framework 2.0 platform, and the second method is applicable to. NET Framework1.1 and 2.0.
Method 1: Suitable for. NET Framework 2.0 platforms
Code
/// <Summary>
/// Start a new program
/// </Summary>
/// <Param name = "Your filepath"> </param>
Private void RunProcess (string encrypted filepath)
{
Process process = new Process ();
Process. StartInfo. FileName = optional filepath;
Process. StartInfo. UseShellExecute = false;
Process. StartInfo. RedirectStandardOutput = true;
Process. OutputDataReceived + = new DataReceivedEventHandler (caseprocess_OutputDataReceived );
Process. Start ();
Process. BeginOutputReadLine ();
// Process. WaitForExit ();
}
/// <Summary>
/// Obtain the Output Response Function
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void caseprocess_OutputDataReceived (object sender, DataReceivedEventArgs e)
{
If (! String. IsNullOrEmpty (e. Data ))
{
Txt. AppendText (e. Data );
Txt. AppendText ("\ r \ n ");
}
}
Method 2: Suitable for. NET Framework 1.1 and 2.0
Code
Private void RunProcess2 (string encrypted filepath)
{
Process process = new Process ();
Process. StartInfo. FileName = optional filepath;
Process. StartInfo. UseShellExecute = false;
Process. StartInfo. RedirectStandardOutput = true;
Process. Start ();
StreamReader reader = process. StandardOutput;
String data = reader. ReadLine ();
While (data! = Null)
{
Txt. AppendText (data );
Txt. AppendText ("\ r \ n ");
Data = reader. ReadLine ();
}
}
Download the test project code
/** // <Summary>
/// Start a new program
/// </Summary>
/// <Param name = "Your filepath"> </param>
Private void RunProcess (string encrypted filepath)
{
Process process = new Process ();
Process. StartInfo. FileName = optional filepath;
Process. StartInfo. UseShellExecute = false;
Process. StartInfo. RedirectStandardOutput = true;
Process. OutputDataReceived + = new DataReceivedEventHandler (caseprocess_OutputDataReceived );
Process. Start ();
Process. BeginOutputReadLine ();
// Process. WaitForExit ();
}
/** // <Summary>
/// Obtain the Output Response Function
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void caseprocess_OutputDataReceived (object sender, DataReceivedEventArgs e)
{
If (! String. IsNullOrEmpty (e. Data ))
{
Txt. AppendText (e. Data );
Txt. AppendText ("\ r \ n ");
}
}