2017-10-06 18:24:02 數據架構師 閱讀數 4785更多
全棧工程師開發手冊 (作者:欒鵬)
c#教程全解
c#執行cmd命令並獲取返回結果字符串
測試代碼
static void Main()
{
string back=execCMD("ipconfig");
System.Console.WriteLine(back);
}
執行cmd命令獲取返回結果字符串函數
public static string execCMD(string command) {
System.Diagnostics.Process pro = new System.Diagnostics.Process();
pro.StartInfo.FileName = "cmd.exe";
pro.StartInfo.UseShellExecute = false;
pro.StartInfo.RedirectStandardError = true;
pro.StartInfo.RedirectStandardInput = true;
pro.StartInfo.RedirectStandardOutput = true;
pro.StartInfo.CreateNoWindow = true;
//pro.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
pro.Start();
pro.StandardInput.WriteLine(command);
pro.StandardInput.WriteLine("exit");
pro.StandardInput.AutoFlush = true;
//獲取cmd窗口的輸出信息
string output = pro.StandardOutput.ReadToEnd();
pro.WaitForExit();//等待程序執行完退出進程
pro.Close();
return output;
}
C#程序調用cmd執行命令
對於C#通過程序來調用cmd命令的操作,網上有很多類似的文章,但很多都不行,竟是漫天的拷貝。我自己測試整理了一下。
代碼:
string str = Console.ReadLine();
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false; //是否使用操作系統shell啟動
p.StartInfo.RedirectStandardInput = true;//接受來自調用程序的輸入信息
p.StartInfo.RedirectStandardOutput = true;//由調用程序獲取輸出信息
p.StartInfo.RedirectStandardError = true;//重定向標准錯誤輸出
p.StartInfo.CreateNoWindow = true;//不顯示程序窗口
p.Start();//啟動程序
//向cmd窗口發送輸入信息
p.StandardInput.WriteLine(str + "&exit");
p.StandardInput.AutoFlush = true;
//p.StandardInput.WriteLine("exit");
//向標准輸入寫入要執行的命令。這里使用&是批處理命令的符號,表示前面一個命令不管是否執行成功都執行后面(exit)命令,如果不執行exit命令,后面調用ReadToEnd()方法會假死
//同類的符號還有&&和||前者表示必須前一個命令執行成功才會執行后面的命令,后者表示必須前一個命令執行失敗才會執行后面的命令
//獲取cmd窗口的輸出信息
string output = p.StandardOutput.ReadToEnd();
//StreamReader reader = p.StandardOutput;
//string line=reader.ReadLine();
//while (!reader.EndOfStream)
//{
// str += line + " ";
// line = reader.ReadLine();
//}
p.WaitForExit();//等待程序執行完退出進程
p.Close();
Console.WriteLine(output);
程序運行結果:

需要提醒注意的一個地方就是:在前面的命令執行完成后,要加exit命令,否則后面調用ReadtoEnd()命令會假死。
我在之前測試的時候沒有加exit命令,輸入其他命令后窗口就假死了,也沒有輸出內容。
對於執行cmd命令時如何以管理員身份運行,可以看我上一篇文章: C#如何以管理員身份運行程序 - 酷小孩 - 博客園
2014-7-28 新增:
另一種C#調用cmd命令的方法,不過這種方法在執行時會“閃一下” 黑窗口,各位在使用時可以按喜好來調用。
/// <summary>
/// 運行cmd命令
/// 會顯示命令窗口
/// </summary>
/// <param name="cmdExe">指定應用程序的完整路徑</param>
/// <param name="cmdStr">執行命令行參數</param>
static bool RunCmd(string cmdExe, string cmdStr)
{
bool result = false;
try
{
using (Process myPro = new Process())
{
//指定啟動進程是調用的應用程序和命令行參數
ProcessStartInfo psi = new ProcessStartInfo(cmdExe, cmdStr);
myPro.StartInfo = psi;
myPro.Start();
myPro.WaitForExit();
result = true;
}
}
catch
{
}
return result;
}
/// <summary>
/// 運行cmd命令
/// 不顯示命令窗口
/// </summary>
/// <param name="cmdExe">指定應用程序的完整路徑</param>
/// <param name="cmdStr">執行命令行參數</param>
static bool RunCmd2(string cmdExe, string cmdStr)
{
bool result = false;
try
{
using (Process myPro = new Process())
{
myPro.StartInfo.FileName = "cmd.exe";
myPro.StartInfo.UseShellExecute = false;
myPro.StartInfo.RedirectStandardInput = true;
myPro.StartInfo.RedirectStandardOutput = true;
myPro.StartInfo.RedirectStandardError = true;
myPro.StartInfo.CreateNoWindow = true;
myPro.Start();
//如果調用程序路徑中有空格時,cmd命令執行失敗,可以用雙引號括起來 ,在這里兩個引號表示一個引號(轉義)
string str = string.Format(@"""{0}"" {1} {2}", cmdExe, cmdStr, "&exit");
myPro.StandardInput.WriteLine(str);
myPro.StandardInput.AutoFlush = true;
myPro.WaitForExit();
result = true;
}
}
catch
{
}
return result;
}
