C# 运行CMD命令

/// <summary>
/// 运行CMD命令
/// </summary>
/// <param name="cmd">命令</param>
/// <returns></returns>
private static void RunCmd(string[] cmd)
{
try
{
// 创建进程
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
process.StandardInput.AutoFlush = true;
for (int i = 0; i < cmd.Length; i++)
{
process.StandardInput.WriteLine(cmd[i]);
}
process.StandardInput.WriteLine("exit");
// 执行进程
string standardOutput = process.StandardOutput.ReadToEnd();
process.WaitForExit();
process.Close();
}
catch (Exception ex)
{
ErrorLogHelper.ErrorInfosDiscribe(new StackFrame(1).GetMethod(), ex.StackTrace, ex.Message);
}
}

}

posted @ 2017-10-09 16:26  随笔`  阅读(955)  评论(0编辑  收藏  举报