DataReceivedEventArgs.Data Proprietà
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Ottiene la riga di caratteri scritti in un flusso di output reindirizzato 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
Valore della proprietà
Riga scritta da un oggetto associato Process al relativo flusso o StandardError reindirizzatoStandardOutput.
Esempio
Nell'esempio di codice seguente viene illustrato un gestore eventi semplice associato all'evento OutputDataReceived . Il gestore eventi riceve righe di testo dal flusso reindirizzato StandardOutput , formatta il testo e scrive il testo sullo schermo.
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
Commenti
Quando si reindirizza il StandardOutput flusso o StandardError di un Process oggetto al gestore eventi, viene generato un evento ogni volta che il processo scrive una riga nel flusso reindirizzato. La Data proprietà è la riga Process scritta nel flusso di output reindirizzato. Il gestore eventi può usare la proprietà per filtrare l'output Data del processo o scrivere l'output in una posizione alternativa. Ad esempio, è possibile creare un gestore eventi che archivia tutte le righe di output degli errori in un file di log degli errori designato.
Una riga viene definita come sequenza di caratteri seguita da un feed di riga ("\n") o da un ritorno a capo immediatamente seguito da un avanzamento riga ("\r\n"). I caratteri di riga vengono codificati usando la tabella codici ANSI di sistema predefinita. La Data proprietà non include il ritorno a capo finale o il feed di riga.
Quando il flusso reindirizzato viene chiuso, viene inviata una riga Null al gestore eventi. Assicurarsi che il gestore eventi controlli la Data proprietà in modo appropriato prima di accedervi. Ad esempio, è possibile usare il metodo String.IsNullOrEmpty statico per convalidare la Data proprietà nel gestore eventi.