通过


DataReceivedEventArgs.Data 属性

定义

获取写入到重定向 Process 输出流的字符行。

public:
 property System::String ^ Data { System::String ^ get(); };
public string? Data { get; }
public string Data { get; }
member this.Data : string
Public ReadOnly Property Data As String

属性值

由关联 Process 到其重定向 StandardOutputStandardError 流编写的行。

示例

下面的代码示例演示了 OutputDataReceived 与该事件关联的简单事件处理程序。 事件处理程序从重定向 StandardOutput 的流接收文本行,设置文本的格式,并将文本写入屏幕。

using System;
using System.IO;
using System.Diagnostics;
using System.Text;

class StandardAsyncOutputExample
{
    private static int lineCount = 0;
    private static StringBuilder output = new StringBuilder();

    public static void Main()
    {
        Process process = new Process();
        process.StartInfo.FileName = "ipconfig.exe";
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
        {
            // Prepend line numbers to each line of the output.
            if (!String.IsNullOrEmpty(e.Data))
            {
                lineCount++;
                output.Append("\n[" + lineCount + "]: " + e.Data);
            }
        });

        process.Start();

        // Asynchronously read the standard output of the spawned process.
        // This raises OutputDataReceived events for each line of output.
        process.BeginOutputReadLine();
        process.WaitForExit();

        // Write the redirected output to this application's window.
        Console.WriteLine(output);

        process.WaitForExit();
        process.Close();

        Console.WriteLine("\n\nPress any key to exit.");
        Console.ReadLine();
    }
}
Imports System.IO
Imports System.Diagnostics
Imports System.Text

Module Module1
    Dim lineCount As Integer = 0
    Dim output As StringBuilder = New StringBuilder()

    Sub Main()
        Dim process As New Process()
        process.StartInfo.FileName = "ipconfig.exe"
        process.StartInfo.UseShellExecute = False
        process.StartInfo.RedirectStandardOutput = True
        AddHandler process.OutputDataReceived, AddressOf OutputHandler
        process.Start()

        ' Asynchronously read the standard output of the spawned process. 
        ' This raises OutputDataReceived events for each line of output.
        process.BeginOutputReadLine()
        process.WaitForExit()

        Console.WriteLine(output)

        process.WaitForExit()
        process.Close()

        Console.WriteLine(Environment.NewLine + Environment.NewLine + "Press any key to exit.")
        Console.ReadLine()
    End Sub

    Sub OutputHandler(sender As Object, e As DataReceivedEventArgs)
        If Not String.IsNullOrEmpty(e.Data) Then
            lineCount += 1

            ' Add the text to the collected output.
            output.Append(Environment.NewLine + "[" + lineCount.ToString() + "]: " + e.Data)
        End If
    End Sub
End Module

注解

将事件或StandardErrorProcess重定向StandardOutput到事件处理程序时,每次进程向重定向流写入一行时都会引发一个事件。 该 Data 属性是写入到重定向输出流的行 Process 。 事件处理程序可以使用该 Data 属性筛选进程输出或将输出写入备用位置。 例如,可以创建一个事件处理程序,将所有错误输出行存储到指定的错误日志文件中。

行被定义为后跟换行符(“\n”)或紧跟换行符(“\r\n”)的字符序列。 行字符使用默认系统 ANSI 代码页进行编码。 该 Data 属性不包括终止回车符或换行符。

当重定向的流关闭时,将空行发送到事件处理程序。 在访问属性之前,请确保事件处理程序 Data 检查该属性。 例如,可以使用静态方法 String.IsNullOrEmpty 验证 Data 事件处理程序中的属性。

适用于