DataReceivedEventArgs.Data DataReceivedEventArgs.Data DataReceivedEventArgs.Data DataReceivedEventArgs.Data Property

定義

リダイレクトされた Process 出力ストリームに書き込まれた文字の行を取得します。Gets the line of characters that was written to a redirected Process output stream.

public:
 property System::String ^ Data { System::String ^ get(); };
public string Data { get; }
member this.Data : string
Public ReadOnly Property Data As String

プロパティ値

関連する Process によって、リダイレクトされた StandardOutput または StandardError ストリームに書き込まれた行。The line that was written by an associated Process to its redirected StandardOutput or StandardError stream.

次のコード例は、 OutputDataReceivedイベントに関連付けられている単純なイベントハンドラーを示しています。The following code example illustrates a simple event handler associated with the OutputDataReceived event. イベントハンドラーは、リダイレクトStandardOutputされたストリームからテキスト行を受け取り、テキストを書式設定して、画面にテキストを書き込みます。The event handler receives text lines from the redirected StandardOutput stream, formats the text, and writes the text to the screen.

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

注釈

StandardOutputまたはStandardErrorProcessストリームをイベントハンドラーにリダイレクトすると、プロセスがリダイレクトされたストリームに行を書き込むたびにイベントが発生します。When you redirect the StandardOutput or StandardError stream of a Process to your event handler, an event is raised each time the process writes a line to the redirected stream. プロパティは、にProcessよってリダイレクトされた出力ストリームに書き込まれた行です。 DataThe Data property is the line that the Process wrote to the redirected output stream. イベントハンドラーは、プロパティをData使用して、プロセスの出力をフィルター処理したり、出力を別の場所に書き込むことができます。Your event handler can use the Data property to filter process output or write output to an alternate location. たとえば、すべてのエラー出力行を指定されたエラーログファイルに格納するイベントハンドラーを作成できます。For example, you might create an event handler that stores all error output lines into a designated error log file.

行は、一連の文字として定義され、その後に改行 ("\n")、またはキャリッジリターンの直後に改行 ("\r\n") が続きます。A line is defined as a sequence of characters followed by a line feed ("\n") or a carriage return immediately followed by a line feed ("\r\n"). 行文字は、既定のシステム ANSI コードページを使用してエンコードされます。The line characters are encoded using the default system ANSI code page. プロパティDataには、終端の復帰またはラインフィードは含まれません。The Data property does not include the terminating carriage return or line feed.

リダイレクトされたストリームが閉じられると、null 行がイベントハンドラーに送信されます。When the redirected stream is closed, a null line is sent to the event handler. アクセスする前に、イベントDataハンドラーがプロパティを適切に確認するようにしてください。Ensure your event handler checks the Data property appropriately before accessing it. たとえば、静的メソッドString.IsNullOrEmptyを使用して、イベントハンドラーのDataプロパティを検証できます。For example, you can use the static method String.IsNullOrEmpty to validate the Data property in your event handler.

適用対象