2015-06-26 29 views
1

我的程序的GUI。 enter image description here如何將參數傳遞給cmd.exe並將結果返回到C#Windows應用程序中

我想通過參數爲DIRCMD.EXE.NETProcess類,並得到正確的輸出到C#程序。

來源:

using System; 
using System.Diagnostics; 
using System.Threading; 
using System.Windows.Forms; 

namespace MyCmd 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      comboBox1.Items.Add("cmd.exe"); 
      comboBox1.SelectedIndex = 0; 
     } 

     private Thread t; 

     private void button1_Click(object sender, EventArgs e) 
     { 
      object[] param = new object[] { comboBox1.Text, txtParams.Text }; 
      if (param.Length > 0 && comboBox1.Text != "") 
      { 
       t = new Thread(() => doTask(param)); 
       t.IsBackground = true; 
       t.Start(); 
      } 
      else 
      { 
       MessageBox.Show("Invalid parameters!"); 
      } 
     } 

     private void doTask(object[] param) 
     { 
      Process proc = new Process 
      { 
       StartInfo = new ProcessStartInfo 
       { 
        FileName = param[0].ToString(), 
        Arguments = param[1].ToString(), 
        UseShellExecute = false, 
        RedirectStandardOutput = true, 
        CreateNoWindow = true 
       } 
      }; 
      proc.Start(); 
      while (!proc.StandardOutput.EndOfStream) 
      { 
       string line = proc.StandardOutput.ReadLine(); 
       this.Invoke((MethodInvoker)delegate 
       { 
        txtResponse.Text += line + Environment.NewLine; 
       }); 
      } 
      proc.WaitForExit(); 
     } 
    } 
} 

這是輸出 enter image description here

我想推出CMD.EXE,然後輸入目錄然後,它列表中的目錄信息到我的應用程序

更新:

來源更新:

using System; 
using System.Diagnostics; 
using System.Threading; 
using System.Windows.Forms; 

namespace MyCmdV2 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      comboBox1.Items.Add("cmd.exe"); 
      comboBox1.SelectedIndex = 0; 
      proc = new Process(); 

      proc.StartInfo = new ProcessStartInfo 
      { 
       FileName = "cmd.exe", 
       UseShellExecute = false, 
       RedirectStandardOutput = true, 
       RedirectStandardInput = true, 
       CreateNoWindow = true 
      }; 
      proc.Start(); 
     } 

     private Process proc; 

     private Thread t; 

     private void button1_Click(object sender, EventArgs e) 
     { 
      t = new Thread(() => doIt()); 
      t.IsBackground = true; 
      t.Start(); 
     } 

     private void doIt() 
     { 
      if (txtParams.Text.ToLower() == "cls") 
      { 
       txtResponse.Text = ""; 
      } 
      else 
      { 
       proc.StandardInput.WriteLine(@txtParams.Text); 
       while (!proc.StandardOutput.EndOfStream) 
       { 
        string line = proc.StandardOutput.ReadLine(); 
        this.Invoke((MethodInvoker)delegate 
        { 
         txtResponse.Text += line + Environment.NewLine; 
        }); 
       } 
       proc.WaitForExit(); 
      } 
     } 
    } 
} 

這是一個例子查詢only.I需要運行任何EXE程序參數。 列出我的所有程序到ComboBox,然後傳遞參數TextBox

有些時候我需要把參數作爲IP這樣的地址。 \\192.168.1.2

Ex: psinfo.exe \\192.168.1.2 

但是C#它的商店爲\\\\192.168.1.2。如何刪除參數中的轉義序列字符。

+0

你有沒有調試過,看看參數是否填入startinfo? –

+0

我已經爲此目的創建了一個類庫:https://github.com/lassevk/commander - 它還沒有nuget包,還沒有到那麼遠,我不認爲它生產就緒我不確定我是否已經測試了所有內容,但您可能希望至少查看那裏的代碼。 –

回答

3

你的代碼看起來很好,因爲它通過「dir」作爲參數。你的代碼和執行「cmd.exe dir」一樣。你是否想啓動cmd.exe然後輸入dir,以便列出目錄結構?如果是這樣,您需要像標準輸出一樣重定向標準輸入,然後將dir寫入輸入流。

UPDATE

代碼來執行你的命令:

private void doTask(object[] param) 
    { 
     Process proc = new Process 
     { 
      StartInfo = new ProcessStartInfo 
      { 
       FileName = param[0].ToString(), 
       //remove this line, it's not needed 
       //Arguments = param[1].ToString(), 
       UseShellExecute = false, 
       RedirectStandardOutput = true, 
       //Add this line so you can write commands 
       RedirectStandardInput = true, 
       CreateNoWindow = true 
      } 
     }; 
     proc.Start(); 

     //now write your command with WriteLine so it mimics an enter press 
     proc.StandardInput.WriteLine(param[1].ToString()); 

     while (!proc.StandardOutput.EndOfStream) 
     { 
      string line = proc.StandardOutput.ReadLine(); 
      this.Invoke((MethodInvoker)delegate 
      { 
       txtResponse.Text += line + Environment.NewLine; 
      }); 
     } 
     proc.WaitForExit(); 
    } 
+0

是的我想啓動cmd.exe,然後輸入dir,以便它將目錄信息列出到我的應用程序中。 – Elshan

+0

@Jimmer更新了我的答案,以顯示如何執行dir命令。在本地測試,它工作正常。 – ManOVision

+0

如何返回回目錄例如:'cmd.exe',然後'cd..' – Elshan

1

看來,標準輸入和輸出已經設置正確。如果你看一下手冊here,你應該更好地瞭解該做什麼。我使用Windows運行功能執行了兩個測試。第一個是cmd dir,與你的一樣,並且失敗慘敗。第二個是cmd /K dir,這產生了我認爲是預期的輸出。使用過的開關使cmd窗口保持打開狀態,查看完成命令後關閉過程的其他開關的手冊。要注意,而大部分在cmd運行事情的程序,

一件事命令,如「目錄」,「CD」是正義的,命令和你不會找到dir.execd.exe任何地方。

0

當您調用cmd.exe時,您需要使用/c參數。進入命令提示符,然後輸入以下:

cmd.exe dir

這主要是你在你的應用程序,它給人以相同的輸出在你的應用程序做什麼。 現在試試:

cmd.exe /c dir

這是你正在尋找的輸出。因此,您通過使用參數/c來告訴cmd.exe運行命令dir

所以,你ProcessStartInfo.Arguments應該是這樣的:

private void doTask(object[] param) 
     { 
      Process proc = new Process 
      { 
       StartInfo = new ProcessStartInfo 
       { 
        FileName = param[0].ToString(), 
        Arguments = "/c " + param[1].ToString(), 
        UseShellExecute = false, 
        RedirectStandardOutput = true, 
        CreateNoWindow = true 
       } 
      }; 
      proc.Start(); 

我已經重新創建您的解決方案,而這按預期工作。

相關問題