Bagikan melalui


DataReceivedEventArgs.Data Properti

Definisi

Mendapatkan baris karakter yang ditulis ke aliran output yang dialihkan 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

Nilai Properti

Baris yang ditulis oleh yang terkait Process dengan yang dialihkan atau StandardError dialirkanStandardOutput.

Contoh

Contoh kode berikut mengilustrasikan penanganan aktivitas sederhana yang terkait dengan peristiwa.OutputDataReceived Penanganan aktivitas menerima baris teks dari aliran yang dialihkan StandardOutput , memformat teks, dan menulis teks ke layar.

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

Keterangan

Saat Anda mengalihkan StandardOutput atau StandardError mengalirkan Process ke penanganan aktivitas Anda, peristiwa dinaikkan setiap kali proses menulis baris ke aliran yang dialihkan. Properti Data adalah baris yang Process ditulis ke aliran output yang dialihkan. Penanganan aktivitas Anda dapat menggunakan Data properti untuk memfilter output proses atau menulis output ke lokasi alternatif. Misalnya, Anda dapat membuat penanganan aktivitas yang menyimpan semua baris output kesalahan ke dalam file log kesalahan yang ditunjuk.

Garis didefinisikan sebagai urutan karakter diikuti oleh umpan baris ("\n") atau pengembalian pengangkutan segera diikuti oleh umpan baris ("\r\n"). Karakter baris dikodekan menggunakan halaman kode ANSI sistem default. Properti Data tidak termasuk penghentian pengembalian pengangkutan atau umpan baris.

Saat aliran yang dialihkan ditutup, baris null dikirim ke penanganan aktivitas. Pastikan penanganan aktivitas Anda memeriksa Data properti dengan tepat sebelum mengaksesnya. Misalnya, Anda dapat menggunakan metode String.IsNullOrEmpty statis untuk memvalidasi Data properti di penanganan aktivitas Anda.

Berlaku untuk