C#执行DOS命令
以重启为例子:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
private void SystemOperate(string strParameter, string strOver) { System.Diagnostics.Process P = new System.Diagnostics.Process(); P.StartInfo.FileName = "cmd.exe"; //设置参数 P.StartInfo.UseShellExecute = false; P.StartInfo.RedirectStandardInput = true; P.StartInfo.RedirectStandardOutput = true; P.StartInfo.CreateNoWindow = true; P.Start(); //执行操作 P.StandardInput.WriteLine(strParameter); P.StandardInput.WriteLine(strOver); P.Close(); }
执行方法,传入重启命令:
MessageBox.Show("系统将重启");
SystemOperate("shutdown -r -t 0", "exit");