2017-11-20 4 views
-1

私はコマンドプロンプトと、それは今、以下のようにProcessの出力をC#変数に取り込む方法は?

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

を出力を与えて、私はC#のプログラムで同じことを模倣しようとしていますし、変数に出力(C:\ProgramData\Windows Fabric\Fabric\log\Traces\)をキャプチャしたいウィンドウにコマンドの下に実行しています。

これを行う方法を

、ここで私が試したコードは、だ

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

ん[この](https://stackoverflow.com/questions/206323/how実行コマンドラインでのc-get-std-out-results?noredirect = 1&lq = 1)のヘルプ? – Stephan

+0

ありがとうStephan .... – user584018

+0

パイプや他のシェル機能を使用するには、 '/ C'オプションで' cmd'を起動する必要があります。 'FabricTraces | findstr Root'はプロセスの引数文字列ではありません... – IllidanS4

答えて

0

このような何か:

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 */ 
} 
関連する問題