C#实现执行CMD命令,实现电脑立即/定时关机
该篇博文主要介绍个人写的一款实现立即与定时关机的软件:
主要是通过调用window下的cmd.exe,然后执行关机相关的cmd命令,实现电脑的立即和定时关机,具体实现如下。
首先是打开系统自带的cmd.exe:
<span style="white-space:pre"> </span> 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();
下面是定时自动关机的实现,跟上面实现方式相同:
<span style="white-space:pre"> </span> 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
时间: 2024-11-05 21:41:53