C#: externe shell-Befehl Ergebnis zeilenweise

Ich Schreibe ein C# - winform-Anwendung, starten Sie einen zweiten Prozess zum ausführen von shell-Kommandos wie "dir" und "ping". Ich leiten den zweiten Prozess der Ausgabe, so dass meine app empfangen werden können Befehl Ergebnis. Es funktioniert grob gesagt in Ordnung.

Das problem ist nur, mein winform-app erhält den Befehl line-Ausgang als ganzes, anstatt Zeile für Zeile. Zum Beispiel, es hat zu warten, bis der externe Befehl "ping" zu beenden (das dauert viele Sekunden oder mehr) und erhält dann die gesamte Ausgabe (viele Linien) auf einmal.

Was ich will, ist die app erhält der cmdline Ausgabe in Echtzeit, d.h. durch Linien, die nicht durch den block. Ist das machbar?

Ich bin mit diesem code, um die Ausgabe Lesen:
while ((Ergebnis = proc.StandardOutput.ReadLine()) != null)

Aber es hat nicht so funktioniert, wie ich es erwartet habe.
Vielen Dank im Voraus.

EDIT: hier ist der code, den ich verwende:

    System.Diagnostics.ProcessStartInfo procStartInfo = new 
            System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);

    procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;

    //The following commands are needed to redirect the standard output.
    procStartInfo.RedirectStandardOutput = true;
    procStartInfo.UseShellExecute = false;
    procStartInfo.CreateNoWindow = true;
    //Now we create a process, assign its ProcessStartInfo and start it
    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo = procStartInfo;
    proc.Start();

    //Get the output into a string
    string result;
    try {
        while ((result = proc.StandardOutput.ReadLine()) != null)
        {
            AppendRtfText(result+"\n", Brushes.Black);
        }
    } //here I expect it to update the text box line by line in real time
      //but it does not.
InformationsquelleAutor Charlie | 2011-08-19
Schreibe einen Kommentar