Start a new program with Process and capture its standard output stream

Source: Internet
Author: User
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 ");
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.