2014-04-16 93 views
0

我想在C#Windows窗體應用程序(不是控制檯之一,帶有選項卡頁面,配置和控制檯作爲列表框)的某種應用程序。輸入進程(批處理文件)

我的問題是,當我寫某種輸入(文本框)時,沒有任何反應(我是編碼新手)。

我的代碼:

Process process = new Process 
      { 
       StartInfo = 
       { 
        FileName = textBox2.Text, 
        //Arguments = textBox3.Text, 
        UseShellExecute = false, 
        RedirectStandardOutput = true, 
        RedirectStandardInput = true, 
        CreateNoWindow = false, 
       } 
      }; 
    server = process; 
    process.Start(); 
    ... 

/* LATER */ 

... 
serverInput = process.StandardInput; 
... 
serverInput.Write(textBoxInput.Text); 

更新 - 解決:代碼:

serverInput.WriteLine(...); 
+0

你開始哪個過程?在textBox2.Text中輸入什麼值? – Hassan

+0

@HassanNisar你可以插入任何東西到textBox2.Text(現在),但你應該有批處理文件的目錄。 – Mibac

回答

0

方法1

這將是足以運行存在於一個批處理文件目錄:

string[] arrBatFiles = Directory.GetFiles(textBox2.Text, "*.bat"); //search at directory path 

//loop through all batch files 
foreach(string sFile in arrBatFiles) 
{ 
    Process.Start(sFile); 
} 

方法2

如果你想使用的ProcessStartInfo成員,使用下面的方法:

public void ExecuteCommand(string sBatchFile, string command) 
{ 
    int ExitCode; 
    ProcessStartInfo ProcessInfo; 
    Process process; 
    string sBatchFilePath = textBox2.Text; //batch file Path 

    ProcessInfo = new ProcessStartInfo(sBatchFile, command); 
    ProcessInfo.CreateNoWindow = false; 
    ProcessInfo.UseShellExecute = false; 
    ProcessInfo.WorkingDirectory = Path.GetDirectoryName(sBatchFile); 
    // *** Redirect the output *** 
    ProcessInfo.RedirectStandardError = true; 
    ProcessInfo.RedirectStandardOutput = true; 

    process = Process.Start(ProcessInfo); 
    process.WaitForExit(); 

    // *** Read the streams *** 
    string sInput = process.StardardInput.ReadToEnd(); 
    string sOutput = process.StandardOutput.ReadToEnd(); 
    string sError = process.StandardError.ReadToEnd(); 

    ExitCode = process.ExitCode; 
} 

UPDATE

當你有目錄如何調用批處理文件。

string[] arrBatFiles = Directory.GetFiles(textBox2.Text, "*.bat"); //search at directory path 

//loop through all batch files 
foreach(string sFile in arrBatFiles) 
{ 
    ExecuteCommand(sFile, string.Empty); //string.Empty refer optional command args 
} 
+0

但是如何執行命令,而進程已經在運行? – Mibac

+0

哪個進程已經運行?你的問題中缺少一些東西。您必須在點擊按鈕或表單加載等事件時調用上述方法。 – Hassan

+0

當進程Example1(在這種情況下表示它是Windows cmd.exe)正在運行時,如何執行命令? (例如按鈕點擊=輸入進程(編程)例如「幫助」) – Mibac