DataReceivedEventArgs.Data Property

Definition

Gets the line of characters that was written to a redirected Process output stream.

C#
public string? Data { get; }
C#
public string Data { get; }

Property Value

The line that was written by an associated Process to its redirected StandardOutput or StandardError stream.

Examples

The following code example illustrates a simple event handler associated with the OutputDataReceived event. The event handler receives text lines from the redirected StandardOutput stream, formats the text, and writes the text to the screen.

C#
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();
    }
}

Remarks

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. The Data property is the line that the Process wrote to the redirected output stream. 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.

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"). The line characters are encoded using the default system ANSI code page. The Data property does not include the terminating carriage return or line feed.

When the redirected stream is closed, a null line is sent to the event handler. Ensure your event handler checks the Data property appropriately before accessing it. For example, you can use the static method String.IsNullOrEmpty to validate the Data property in your event handler.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1