DataReceivedEventArgs.Data Właściwość

Definicja

Pobiera wiersz znaków, który został zapisany do przekierowanego Process strumienia wyjściowego.

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

Wartość właściwości

Wiersz, który został napisany przez skojarzony z Process przekierowanym StandardOutput lub StandardError strumieniem.

Przykłady

Poniższy przykład kodu ilustruje prostą procedurę obsługi zdarzeń skojarzoną ze zdarzeniem OutputDataReceived . Procedura obsługi zdarzeń odbiera wiersze tekstowe ze strumienia przekierowanego StandardOutput , formatuje tekst i zapisuje tekst na ekranie.

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

Uwagi

Po przekierowaniu strumienia StandardOutput lub StandardError do programu obsługi zdarzeń Process zdarzenie jest wywoływane za każdym razem, gdy proces zapisuje wiersz do przekierowanego strumienia. Właściwość Data jest wierszem napisanym Process do przekierowanego strumienia wyjściowego. Procedura obsługi zdarzeń może używać Data właściwości do filtrowania danych wyjściowych procesu lub zapisu danych wyjściowych w lokalizacji alternatywnej. Można na przykład utworzyć procedurę obsługi zdarzeń, która przechowuje wszystkie wiersze wyjściowe błędów w wyznaczonym pliku dziennika błędów.

Wiersz jest definiowany jako sekwencja znaków, po której następuje kanał informacyjny wiersza ("\n") lub powrót karetki bezpośrednio po wierszu ("\r\n"). Znaki wiersza są kodowane przy użyciu domyślnej strony kodowej ANSI. Właściwość Data nie zawiera powrotu karetki zakończenia ani kanału informacyjnego wiersza.

Po zamknięciu przekierowanego strumienia do programu obsługi zdarzeń jest wysyłany wiersz o wartości null. Upewnij się, że program obsługi zdarzeń sprawdza Data właściwość odpowiednio przed uzyskaniem do niego dostępu. Na przykład możesz użyć metody String.IsNullOrEmpty statycznej, aby zweryfikować Data właściwość w procedurze obsługi zdarzeń.

Dotyczy