using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CloseWinTimer
{
class ComputerOperator
{
public bool ExecOperate(int type)
{
Process p = new Process();
switch (type)
{
case 1:
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
try
{
p.Start();
p.StandardInput.WriteLine("Shutdown.exe -s -f -t 10 "); //关机命令
p.WaitForExit();
p.Close();
return true;
}
catch (Exception ex)
{
MessageBox.Show("因异常:" + ex.Message + "\n\n关机执行失败");
return false;
}
case 2:
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
try
{
p.Start();
p.StandardInput.WriteLine("Shutdown.exe -r -f -t 10 "); //重启命令
p.WaitForExit();
p.Close();
return true;
}
catch (Exception ex)
{
MessageBox.Show("因异常:" + ex.Message + "\n\n重启执行失败");
return false;
}
case 3:
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
try
{
p.Start();
p.StandardInput.WriteLine("shutdown -a "); //重启命令
p.WaitForExit();
p.Close();
return true;
}
catch (Exception ex)
{
MessageBox.Show("因异常:" + ex.Message + "\n\n取消失败");
return false;
}
}
return false;
}
}
}
本文介绍了一个使用C#编写的程序,该程序能够实现定时执行计算机的关机、重启或取消关机操作。通过调用Shutdown.exe命令并利用Process类来控制执行流程,程序提供了灵活的操作选项。
417

被折叠的 条评论
为什么被折叠?



