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

注釈

または StandardErrorProcessストリームをイベント ハンドラーにリダイレクトStandardOutputすると、プロセスがリダイレクトされたストリームに行を書き込むたびにイベントが発生します。 プロパティは Data 、 が Process リダイレクトされた出力ストリームに書き込んだ行です。 イベント ハンドラーでは、 プロパティを Data 使用して、プロセス出力をフィルター処理したり、別の場所に出力を書き込んだりできます。 たとえば、すべてのエラー出力行を指定されたエラー ログ ファイルに格納するイベント ハンドラーを作成できます。

行は、一連の文字の後に改行 ("\n") またはキャリッジ リターンの直後に改行 ("\r\n") として定義されます。 行文字は、既定のシステム ANSI コード ページを使用してエンコードされます。 プロパティには Data 、終端キャリッジ リターンまたはライン フィードは含まれません。

リダイレクトされたストリームが閉じられると、null 行がイベント ハンドラーに送信されます。 イベント ハンドラーがプロパティにアクセスする前に、プロパティを Data 適切にチェックしていることを確認します。 たとえば、静的メソッド String.IsNullOrEmpty を使用して、イベント ハンドラーで プロパティを検証 Data できます。

適用対象