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

속성 값

연결된 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

설명

의 또는 StandardError 스트림을 Process 이벤트 처리기로 리디렉션 StandardOutput 하면 프로세스가 리디렉션된 스트림에 줄을 쓸 때마다 이벤트가 발생합니다. 속성은 Data 리디렉션된 출력 스트림에 쓴 줄 Process 입니다. 이벤트 처리기는 속성을 사용하여 프로세스 출력을 Data 필터링하거나 대체 위치에 출력을 쓸 수 있습니다. 예를 들어 모든 오류 출력 줄을 지정된 오류 로그 파일에 저장하는 이벤트 처리기를 만들 수 있습니다.

줄은 문자 시퀀스 뒤에 줄 바꿈("\n") 또는 줄 바꿈("\r\n") 바로 뒤에 캐리지 리턴으로 정의됩니다. 줄 문자는 기본 시스템 ANSI 코드 페이지를 사용하여 인코딩됩니다. 속성에는 Data 종료 캐리지 리턴 또는 줄 바꿈이 포함되지 않습니다.

리디렉션된 스트림이 닫혀 있으면 null 줄이 이벤트 처리기로 전송됩니다. 이벤트 처리기는 액세스하기 전에 속성을 적절하게 확인 Data 합니다. 예를 들어 정적 메서드 String.IsNullOrEmpty 를 사용하여 이벤트 처리기에서 속성의 유효성을 Data 검사할 수 있습니다.

적용 대상