2014-09-02 215 views
0

我正嘗試創建一個啓動cmd.exe併發送命令的應用程序。命令在cmd上可見是很重要的。這是我迄今爲止,但它似乎並沒有工作。任何想法?C#在命令提示符下運行命令並顯示命令

  Process myProc = new Process(); 
      myProc.StartInfo.FileName = "cmd.exe"; 
      myProc.StartInfo.RedirectStandardInput = true; 
      myProc.StartInfo.RedirectStandardOutput = true; 
      myProc.StartInfo.UseShellExecute = false; 
      myProc.Start(); 
      StreamWriter sendCommand = myProc.StandardInput; 
      sendCommand.WriteLine("run.exe --forever"); //I want this command to show up in cmd 

當上面的代碼執行時,run.exe運行,但命令不顯示在cmd中。 我在做什麼錯?

+2

你可能沒有看到它,因爲你已經重定向標準輸出。試着評論一下,看看會發生什麼。 – tinstaafl 2014-09-02 20:50:02

+0

無法在沒有重定向的情況下運行StandardInput。 – 2014-09-03 12:43:45

+0

未輸入輸出。子進程的輸出被重定向回調用進程,但你沒有做任何事情。您不需要爲了使用輸入流而重定向兩個流。 – tinstaafl 2014-09-03 14:51:50

回答

0

這裏是一個增編我的意見,以使其更清晰:

Process myProc = new Process(); 
myProc.StartInfo.FileName = "cmd.exe"; 
myProc.StartInfo.RedirectStandardInput = true; 
//myProc.StartInfo.RedirectStandardOutput = true; 
myProc.StartInfo.UseShellExecute = false; 
myProc.Start(); 
System.IO.StreamWriter sendCommand = myProc.StandardInput; 
sendCommand.WriteLine("run.exe --forever"); 

這將允許cmd輸出一切都在cmd控制檯顯示。

0

你爲什麼使用streamwriter? 您可以使用參數

myProc.StartInfo.Arguments="run.exe --forever";