2014-07-09 47 views
2

我想發送帶參數的命令並從cmd中讀取它們的答案。所以,我寫了下面的代碼,但它不工作並鎖定在屏幕上(myString通常爲空 - 「」)。我只想將命令發送到打開的命令提示符。哪裏有問題?提前致謝。 (例如:怎樣取ping請求的結果?)發送cmd命令並在C#中讀取結果

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Diagnostics; 
using System.IO; 

namespace CallBatchFile 
{ 
    class Program 
    { 
     [STAThread] 
     static void Main() 
     {    

      Process p = new Process(); 
      p.StartInfo.FileName = "cmd.exe"; 
      p.StartInfo.Arguments = "/c date"; 
      p.StartInfo.UseShellExecute = false; 
      p.StartInfo.RedirectStandardError = true; 
      p.StartInfo.CreateNoWindow = true; 
      p.StartInfo.RedirectStandardOutput = true; 
      p.Start(); 

      string myString = p.StandardOutput.ReadToEnd(); 
      p.WaitForExit(); 
     } 
    } 
} 
+1

[Q1](http://stackoverflow.com/questions/11060019/command-prompt-output-being-read-as-empty-string)[Q2](http://www.codeproject.com/Articles/ 16081/CommandLineHelper-class-to-launch-console-applicat)可能會有所幫助。 –

+0

嘗試此代碼並按預期工作。但是,如果沒有/ T標誌,命令shell會提示您插入新的日期。 – Steve

回答

1

cmd /c date阻塞。您可以使用

p.StartInfo.Arguments = "/c date /T"; 

要停止日期等輸入,或者給輸入cmd

p.StartInfo.RedirectStandardInput = true; 
... 
p.StandardInput.Write("\n"); 

..或讀異步這樣你就可以得到輸出,而CMD正在等待您的輸入:

p.BeginOutputReadLine(); 
p.OutputDataReceived += (_, e) => Console.WriteLine(e.Data); 
0

此代碼可以幫助你

string strOutput; 
    //Starting Information for process like its path, use system shell i.e. control process by system etc. 
    ProcessStartInfo psi = new ProcessStartInfo(@"C:\WINDOWS\system32\cmd.exe"); 
    // its states that system shell will not be used to control the process instead program will handle the process 
    psi.UseShellExecute = false; 
    psi.ErrorDialog = false; 
    // Do not show command prompt window separately 
    psi.CreateNoWindow = true; 
    psi.WindowStyle = ProcessWindowStyle.Hidden; 
    //redirect all standard inout to program 
    psi.RedirectStandardError = true; 
    psi.RedirectStandardInput = true; 
    psi.RedirectStandardOutput = true; 
    //create the process with above infor and start it 
    Process plinkProcess = new Process(); 
    plinkProcess.StartInfo = psi; 
    plinkProcess.Start(); 
    //link the streams to standard inout of process 
    StreamWriter inputWriter = plinkProcess.StandardInput; 
    StreamReader outputReader = plinkProcess.StandardOutput; 
    StreamReader errorReader = plinkProcess.StandardError; 
    //send command to cmd prompt and wait for command to execute with thread sleep 
    inputWriter.WriteLine("C:\\PLINK -ssh [email protected] -pw opensuselinux echo $SHELL\r\n"); 
    Thread.Sleep(2000); 
    // flush the input stream before sending exit command to end process for any unwanted characters 
    inputWriter.Flush(); 
    inputWriter.WriteLine("exit\r\n"); 
    // read till end the stream into string 
    strOutput = outputReader.ReadToEnd(); 
    //remove the part of string which is not needed 
    int val = strOutput.IndexOf("-type\r\n"); 
    strOutput = strOutput.Substring(val + 7); 
    val = strOutput.IndexOf("\r\n"); 
    strOutput = strOutput.Substring(0, val); 
    MessageBox.Show(strOutput);