This documentation is archived and is not being maintained.

DataReceivedEventArgs.Data Property

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

Namespace:  System.Diagnostics
Assembly:  System (in System.dll)

public string Data { get; }

Property Value

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

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.

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.

private static void SortOutputHandler(object sendingProcess, 
    DataReceivedEventArgs outLine)
{
    // Collect the sort command output. 
    if (!String.IsNullOrEmpty(outLine.Data))
    {
        numOutputLines++;

        // Add the text to the collected output.
        sortOutput.Append(Environment.NewLine + 
            "[" + numOutputLines.ToString() + "] - " + outLine.Data);
    }
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Show: