DataReceivedEventArgs.Data 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得已寫入至重新導向 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 寫入至其重新導向的 StandardOutput 或 StandardError 資料流的字行。
範例
下列程式代碼範例說明與 事件相關聯的 OutputDataReceived 簡單事件處理程式。 事件處理程式會從重新導向 StandardOutput 的數據流接收文字行、格式化文字,並將文字寫入畫面。
using namespace System;
using namespace System::IO;
using namespace System::Diagnostics;
using namespace System::Text;
ref class StandardAsyncOutputExample
{
private:
static int lineCount = 0;
static StringBuilder^ output = nullptr;
public:
static void Run()
{
Process^ process = gcnew Process();
process->StartInfo->FileName = "ipconfig.exe";
process->StartInfo->UseShellExecute = false;
process->StartInfo->RedirectStandardOutput = true;
output = gcnew StringBuilder();
process->OutputDataReceived += gcnew DataReceivedEventHandler(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();
// 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();
}
private:
static void OutputHandler(Object^ sender, DataReceivedEventArgs^ e)
{
// Prepend line numbers to each line of the output.
if (!String::IsNullOrEmpty(e->Data))
{
lineCount++;
output->Append("\n[" + lineCount + "]: " + e->Data);
}
}
};
int main()
{
StandardAsyncOutputExample::Run();
}
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
備註
當您將 StandardOutput 的 或 StandardError 數據流 Process 重新導向至事件處理程式時,每次進程將一行寫入重新導向的數據流時,就會引發事件。 屬性 Data 是寫入至重新導向輸出數據流的行 Process 。 事件處理程式可以使用 Data 屬性來篩選進程輸出,或將輸出寫入替代位置。 例如,您可以建立事件處理程式,將所有錯誤輸出行儲存到指定的錯誤記錄檔中。
行定義為字元序列,後面接著換行字元 (“\n”) ,或緊接著換行字元 (“\r\n”) 。 行字元會使用預設系統 ANSI 代碼頁進行編碼。 屬性 Data 不包含終止歸位字元或換行字元。
當重新導向的數據流關閉時,會將 Null 行傳送至事件處理程式。 在存取屬性之前, Data 請確定事件處理程式會先適當地檢查屬性。 例如,您可以使用靜態方法來 String.IsNullOrEmpty 驗證 Data 事件處理程式中的屬性。