2010-11-24 4 views
0

私がやりたいことは、コンソールコマンドウィンドウからC#でコマンドを実行することです。 このコマンドを実行するには、指定した入力で既存のexeファイルを実行し、出力を別のファイルに出力します。C#でコマンドを実行する

私はこのコードを持っている:

System.Diagnostics.Process pProcess = new System.Diagnostics.Process(); 
string l_sCommand = string.Empty; 
l_sCommand += "exe file" + "<" + "Input txt file" + ">" + "output txt file"; 

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

System.Diagnostics.Process proc = new System.Diagnostics.Process(); 
proc.StartInfo = procStartInfo; 
proc.Start(); 

をしかし、これは動作しません。 誰にも理由が分かりますか?

+4

正確に「機能しない」とは何ですか? –

+3

具体的にはどのように機能しないのですか?代わりに何が起こるのですか? – thecoop

+0

ストリームを使用してリダイレクトを自分で処理しないのはなぜですか?それとも、あなたが求めていることですか? – Rup

答えて

0

はなぜ使用しない:あなたはCMD.EXEを起動しようとしている

System.Diagnostics.Process.Start("<exe file>", "<args>");

?コマンドラインアプリの場合は、Process.Start静的メソッドからEXEを起動することができます。

+0

いいえ、入力ファイルはユーザーからの入力をシミュレートします。 – Yarden

+0

私は別の方法で質問します。 バッチファイルを実行しているかのようにコマンドを実行したいのですが、バッチファイルを作成したくありません。 " – Yarden

+0

私の現在の状況はどうですか?出力ファイルを作成していないのですか? –

0

使用Process.Start方法直接引数を渡すか、

0

通常あなただけのProcess.Startメソッドに実行可能ファイル名を渡すか、Process.StartInfoプロパティを使用して組み込みShell関数を使用します。しかし、標準出力をリダイレクトする場合は、Process.StartInfo.RedirectStandardOutputをtrueに設定し、プロセスが終了するとProcess.StandardOutputを読み込む必要があります。下記のMSDNのコードリフターのサンプルを参照してください。

// Start the child process. 
Process p = new Process(); 
// Redirect the output stream of the child process. 
p.StartInfo.UseShellExecute = false; 
p.StartInfo.RedirectStandardOutput = true; 
p.StartInfo.FileName = "Write500Lines.exe"; 
p.Start(); 
// Do not wait for the child process to exit before 
// reading to the end of its redirected stream. 
// p.WaitForExit(); 
// Read the output stream first and then wait. 
string output = p.StandardOutput.ReadToEnd(); 
p.WaitForExit(); 

詳細については、このリンクのドキュメントを参照してください。 http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx

0

実際によくある問題です。 Processクラスはそれほど使いやすいものではありません。 (this postを参照)ProcessRunner class I wroteを使用すると、次のコードが機能します。

using CSharpTest.Net.Processes; 
void Exec(string outputFile, Encoding encoding, string program, params string[] args) 
{ 
    using (TextWriter output = new StreamWriter(outputFile, false, encoding)) 
    using (ProcessRunner pi = new ProcessRunner(program)) 
    { 
     pi.OutputReceived += delegate(object sender, ProcessOutputEventArgs e) 
           { output.WriteLine(e.Data); }; 
     pi.Run(args); 
    } 
} 
0

出力をバッファにストリームしてから自分で書き込んでみませんか?

public void CaptureProcess(String Command, String Arguments, String Filename) 
{ 
    // This is the code for the base process 
    Process myProcess = new Process(); 
    myProcess.StartInfo = new ProcessStartInfo(Command, Arguments); 
    myProcess.StartInfo.UseShellExecute = false; 
    myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
    myProcess.StartInfo.RedirectStandardOutput = true; 

    // Open a filestream for the output 
    using (FileStream fs = new FileStream(Filename, FileMode.OpenOrCreate)) 
    { 
     using (StreamWriter sw = new StreamWriter(fs)) 
     { 

      // open the stream and capture all output from the process until it dies. 
      using (StreamReader ProcessOutput = myProcess.StandardOutput) 
      { 
       myProcess.Start(); 
       string output = ProcessOutput.ReadToEnd(); 
       sw.Write(output); 

       myProcess.WaitForExit(); 
       myProcess.Close(); 
      } 
     } 
    } 
} 
関連する問題