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;
}
}
}