共用方式為


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

屬性值

由與其重定向StandardOutputStandardError或串流相關的Process一行所寫。

範例

以下程式碼範例說明與事件相關的 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

備註

當你將 StandardOutputProcessStandardError 串流重新導向到你的事件處理器時,每次程序寫入一行到被導向的串流時,都會產生一個事件。 屬性 Data 是寫 Process 入重定向輸出串流的那行。 你的事件處理器可以使用這個 Data 屬性來過濾程序輸出或寫入其他位置。 例如,你可以建立一個事件處理程序,將所有錯誤輸出行儲存在指定的錯誤日誌檔中。

一行定義為一連串字元後接換行(「\n」),或是回車接換行(「\r\n」)。 行字元使用系統預設的 ANSI 代碼頁編碼。 該 Data 屬性不包含終端的回車或換行。

當重定向的串流被關閉時,會傳送一行空線到事件處理程序。 確保你的事件處理員在進入物業前妥 Data 善檢查該物業。 例如,你可以用靜態方法 String.IsNullOrEmpty 在事件處理器中驗證該 Data 屬性。

適用於