DataReceivedEventArgs.Data Propriété
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Obtient la ligne de caractères qui a été écrite dans un flux de sortie redirigé 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
Valeur de propriété
Ligne écrite par un associé Process à son flux ou StandardError à son flux redirigéStandardOutput.
Exemples
L’exemple de code suivant illustre un gestionnaire d’événements simple associé à l’événement OutputDataReceived . Le gestionnaire d’événements reçoit des lignes de texte du flux redirigé StandardOutput , met en forme le texte et écrit le texte à l’écran.
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
Remarques
Lorsque vous redirigez le flux ou StandardError le StandardOutput flux d’un Process vers votre gestionnaire d’événements, un événement est déclenché chaque fois que le processus écrit une ligne dans le flux redirigé. La Data propriété est la ligne écrite Process dans le flux de sortie redirigé. Votre gestionnaire d’événements peut utiliser la propriété pour filtrer la Data sortie du processus ou écrire la sortie dans un autre emplacement. Par exemple, vous pouvez créer un gestionnaire d’événements qui stocke toutes les lignes de sortie d’erreur dans un fichier journal d’erreurs désigné.
Une ligne est définie comme une séquence de caractères suivie d’un flux de ligne («\n») ou d’un retour chariot immédiatement suivi d’un flux de ligne («\r\n»). Les caractères de ligne sont encodés à l’aide de la page de codes ANSI système par défaut. La Data propriété n’inclut pas le retour chariot de fin ou le flux de ligne.
Lorsque le flux redirigé est fermé, une ligne Null est envoyée au gestionnaire d’événements. Vérifiez que votre gestionnaire d’événements vérifie correctement la Data propriété avant de l’accéder. Par exemple, vous pouvez utiliser la méthode String.IsNullOrEmpty statique pour valider la Data propriété dans votre gestionnaire d’événements.