using System.Diagnostics; 添加此命名空间,此有定义进程的类。
执行方式:
Process p = new Process(); //新建一个进程
以下是对于新建进程启动的基本设置:
p.StartInfo.FileName = "cmd.exe"; //进程的启动名称,标志它是一个可执行程序。
p.StartInfo.UseShellExecute = true;//是否启动系统外壳选
(UseShellExecute=false 不用系统默认的程序打开,所以在你没有指定用什么程序打开时,线程就一直等待。
而UserShellExecute默认值是true,所以你注释掉时,它会用默认的程序打开你需要打开的文件,便能继续执行下去。UseShellExecute是否从控制台启动。如果想从标准输出流中读取输出,这个属性必需设为False
若要使用 StandardOutput,必须将 ProcessStartInfo.UseShellExecute 设置为 false,并且将ProcessStartInfo.RedirectStandardOutput 设置为 true)
p.StartInfo.RedirectStandardInput = true;//这是是否从StandardInput输入
p.StartInfo.CreateNoWindow = true;//这里是启动程序是否显示窗体
启动执行过程:
p.Start();//启动进程
p.StandardInput.WriteLine("shutdown -s -t 10");//运行关机命令shutdown (-s)是关机 (-t)是延迟的时间 这里用秒计算 10就是10秒后关机