C#执行cmd命令实现电脑关机

时间:2022-09-02 20:29:31
C#实现执行CMD命令,实现电脑立即/定时关机
该篇博文主要介绍个人写的一款实现立即与定时关机的软件: C#执行cmd命令实现电脑关机
主要是通过调用window下的cmd.exe,然后执行关机相关的cmd命令,实现电脑的立即和定时关机,具体实现如下。 首先是打开系统自带的cmd.exe:
Process process = new 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();
.Start以后便可输入cmd命令执行:
process.StandardInput.WriteLine("shutdown -s -t 3");
process.StandardInput.WriteLine("exit");
Shuttime = System.Int32.Parse(Intime.Text);
process.WaitForExit();
process.Close();

下面是定时自动关机的实现,跟上面实现方式相同:
            string time;
time = Intime.Text;
Process process = new 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.WriteLine("shutdown -s -t " + time);
process.StandardInput.WriteLine("exit");
Shuttime = System.Int32.Parse(Intime.Text);
process.WaitForExit();
process.Close();
其中time是获取到的输入的定时关机时间。

源码资源:http://download.csdn.net/detail/laozhuxinlu/9512547