Process p = new Process();
//设置要启动的应用程序
p.StartInfo.FileName = "cmd.exe";
//是否使用操作系统shell启动
p.StartInfo.UseShellExecute = false;
// 接受来自调用程序的输入信息
p.StartInfo.RedirectStandardInput = true;
// 设置不输出信息
p.StartInfo.RedirectStandardOutput = false;
// 输出错误
p.StartInfo.RedirectStandardError = true;
//不显示程序窗口
p.StartInfo.CreateNoWindow = true;
try
{
//启动程序
p.Start();
//向cmd窗口发送输入信息
p.StandardInput.WriteLine("命令字符串");
//自动处理缓存数据
p.StandardInput.AutoFlush = true;
//退出命令窗口
p.StandardInput.WriteLine("exit");
//等待程序执行完退出进程
p.WaitForExit();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (p != null)
{
p.Close();
}
}