DataReceivedEventArgs.Data Özellik

Tanım

Yeniden yönlendirilen Process çıkış akışına yazılan karakter satırını alır.

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

Özellik Değeri

Yeniden yönlendirilen StandardOutput veya StandardError akışıyla ilişkili Process bir tarafından yazılan satır.

Örnekler

Aşağıdaki kod örneği, olayla ilişkili basit bir olay işleyicisini OutputDataReceived gösterir. Olay işleyicisi, yeniden yönlendirilen StandardOutput akıştan metin satırları alır, metni biçimlendirer ve metni ekrana yazar.

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

Açıklamalar

veya StandardError akışını Process olay işleyicinize yeniden yönlendirdiğinizdeStandardOutput, işlem yeniden yönlendirilen akışa her satır yazdığında bir olay oluşturulur. Data özelliği, yeniden yönlendirilen çıkış akışına yazan satırdırProcess. Olay işleyiciniz, işlem çıkışını Data filtrelemek veya çıkışı alternatif bir konuma yazmak için özelliğini kullanabilir. Örneğin, tüm hata çıkış satırlarını belirlenmiş bir hata günlüğü dosyasında depolayan bir olay işleyicisi oluşturabilirsiniz.

Satır, ardından bir satır akışı ("\n") veya satır beslemesi ("\r\n") hemen ardından satır başı gelen bir karakter dizisi olarak tanımlanır. Satır karakterleri varsayılan sistem ANSI kod sayfası kullanılarak kodlanır. Data özelliği sonlandırıcı satır dönüşünü veya satır beslemesini içermez.

Yeniden yönlendirilen akış kapatıldığında, olay işleyicisine null bir satır gönderilir. Olay işleyicinizin, özelliği erişmeden önce uygun şekilde denetlediğinden Data emin olun. Örneğin, olay işleyicinizdeki özelliği doğrulamak Data için statik yöntemini String.IsNullOrEmpty kullanabilirsiniz.

Şunlara uygulanır