2017-11-20 2 views
-1

ich in den Fenstern unter Befehl bin mit Eingabeaufforderung, und es wird eine Ausgabe wie unten geben,Wie erfasst man die Ausgabe für Process in eine C# -Variable?

C:\>logman.exe FabricTraces | findstr Root 
Root Path: C:\ProgramData\Windows Fabric\Fabric\log\Traces\ 

Nun, ich versuche, das gleiche in C# Programm zu imitieren und möchte den Ausgang (C:\ProgramData\Windows Fabric\Fabric\log\Traces\) in eine Variable erfassen .

Wie dies zu tun, hier ist der Code, den ich versuchte,

Process P = Process.Start("logman.exe", "FabricTraces | findstr Root"); 
      P.WaitForExit(); 
      var result = P.ExitCode; 
+2

tut [diese] (https://stackoverflow.com/questions/206323/how -to-execute-Kommandozeile-in-c-get-std-out-Ergebnisse? noredirect = 1 & lq = 1) Hilfe? – Stephan

+0

Vielen Dank Stephan .... – user584018

+0

Sie müssen 'cmd' mit der'/C' Option starten, wenn Sie Pipes und andere Shell Features benutzen wollen. 'FabricTraces | findstr Root' ist nicht die Argumentkette des Prozesses ... – IllidanS4

Antwort

0

Etwas wie folgt aus:

private void StartProcess() 
{ 
    System.Diagnostics.Process process = new System.Diagnostics.Process(); 

    process.StartInfo.FileName    = /* path + binary */; 
    process.StartInfo.Arguments    = /* arguments */; 
    process.StartInfo.WorkingDirectory  = /* working directory */; 
    process.StartInfo.RedirectStandardOutput = true; 
    process.StartInfo.RedirectStandardError = true; 
    process.StartInfo.UseShellExecute  = false; 
    process.StartInfo.CreateNoWindow   = true; 

    process.OutputDataReceived += Process_OutputDataReceived; 
    process.ErrorDataReceived += Process_ErrorDataReceived; 

    process.Start(); 

    process.BeginOutputReadLine(); 
    process.BeginErrorReadLine(); 

    process.WaitForExit(); 
} 

private void Process_ErrorDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e) 
{ 
    /* e.Data will contain string with error message */ 
} 

private void Process_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e) 
{ 
    /* e.Data will contain string with output */ 
} 
Verwandte Themen