我正在嘗試從代碼與Windows命令提示符進行交互。我的目標是顯示提示,輸入一些命令,顯示輸出,然後重複。但似乎無法同時獲得前三名工作。如何與C#代碼中的命令提示符進行交互?
private void button2_Click(object sender, EventArgs e)
{
Process proc = new Process();
proc.StartInfo.FileName = "cmd";
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardInput = true;
proc.Start();
StreamReader k = new StreamReader(proc.StandardOutput.BaseStream);
textBox2.Text = k.ReadToEnd();
}
上面的代碼只是掛起。如果我沒有設置RedirectStandardInput,那麼我可以顯示整個提示。如果我關閉StandardInput的作者,我可以得到一個命令來運行,但沒有看到提示,或者無法重複。那麼我怎樣才能得到提示來顯示,運行一個命令並重復顯示輸出?
我希望能夠重現Console2的命令行行爲,但我有點不知所措的源代碼。