我試圖從C#運行命令行腳本。我希望它在沒有shell的情況下運行,並將輸出放入我的字符串輸出中。它不喜歡p.StartInfo行。我究竟做錯了什麼?我沒有運行像p.StartInfo.FileName =「YOURBATCHFILE.bat」的文件,如How To: Execute command line in C#, get STD OUT results。我需要設置「CMD.exe」和命令行字符串。我試過p.Start(「CMD.exe」,strCmdText);但是這給了我錯誤:「Memer'System.Diagnostics.Process.Start(string,string)'不能使用實例引用進行訪問;請使用類型名稱對其進行限定。」從沒有窗口的C#運行命令行並獲取輸出
string ipAddress;
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
string strCmdText;
strCmdText = "tracert -d " + ipAdress;
p.StartInfo("CMD.exe", strCmdText);
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
你可以提供一個程序給我們建立?我99%肯定這個不會 –
「它不喜歡p.StartInfo行。」究竟是什麼錯誤? – kjbartel
不,它不會運行,因爲IP地址是特定於我的機器的,並且p.StartInfo也不會編譯。它說它「不能像方法一樣使用」。 – Sean