功能:自动设置电脑关机时间,可取消
创建一个shutdown函数,bool isCancle,传入值为ture就取消关机,interval间隔时间,unit不带符号的整型
private static void ShutdownPC(bool isCancel, uint interval)
{
Process proc = new Process();
proc.StartInfo.FileName = "cmd.exe"; // 启动命令行程序
proc.StartInfo.UseShellExecute = false; // 不使用Shell来执行,用程序来执行
proc.StartInfo.RedirectStandardError = true; // 重定向标准输入输出
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.CreateNoWindow = true; // 执行时不创建新窗口
proc.Start();
string commandLine;
if (isCancel)
commandLine = @"shutdown /a";//取消自动关机命令
else
commandLine = @"shutdown /s /t " + interval.ToString();
proc.StandardInput.WriteLine(commandLine);
}
1个时间选择器,2个按钮,一个确定按钮一个取消按钮
当默认时间小于设定时间的话,提示错误
/// <summary>
/// 确定按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_sure_Click(object sender, EventArgs e)
{
//延迟关机,等待的时间
int waitSecond = 0;
DateTime currentTime = DateTime.Now.ToLocalTime();//获取当前时间
DateTime aimTime = dateTimePicker_aim.Value;//获取目标时间
//比较两个时间。关机时间也在当前时间之后
if (DateTime.Compare(currentTime, aimTime) >= 0)
{
MessageBox.Show("目标时间小于当前时间");
return;
}
else{
//将两个时间间隔,换算成秒,取整输出
int AimCompareCurrentToSecond = ((int)((aimTime - currentTime).TotalSeconds));
waitSecond = AimCompareCurrentToSecond + 1;
//MessageBox.Show(AimCompareCurrentToSecond.ToString());
}
//设置自动关机
ShutdownPC(false, (uint)waitSecond);
label_current.Text = "已设置自动关机";
button_cancel.Enabled = true;
button_sure.Enabled = false;
}
/// <summary>
/// 取消关机按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_cancel_Click(object sender, EventArgs e)
{
ShutdownPC(true, 0);//true取消自动关机,0关机延迟时间
label_current.Text = "无自动关机";
button_cancel.Enabled = false;
button_sure.Enabled = true;
}
界面效果:
完整源码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
namespace Auto_shut
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private static void ShutdownPC(bool isCancel, uint interval)
{
Process proc = new Process();
proc.StartInfo.FileName = "cmd.exe"; // 启动命令行程序
proc.StartInfo.UseShellExecute = false; // 不使用Shell来执行,用程序来执行
proc.StartInfo.RedirectStandardError = true; // 重定向标准输入输出
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.CreateNoWindow = true; // 执行时不创建新窗口
proc.Start();
string commandLine;
if (isCancel)
commandLine = @"shutdown /a";//取消自动关机命令
else
commandLine = @"shutdown /s /t " + interval.ToString();
proc.StandardInput.WriteLine(commandLine);
}
/// <summary>
/// 确定按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_sure_Click(object sender, EventArgs e)
{
//延迟关机,等待的时间
int waitSecond = 0;
DateTime currentTime = DateTime.Now.ToLocalTime();//获取当前时间
DateTime aimTime = dateTimePicker_aim.Value;//获取目标时间
//比较两个时间。关机时间也在当前时间之后
if (DateTime.Compare(currentTime, aimTime) >= 0)
{
MessageBox.Show("目标时间小于当前时间");
return;
}
else{
//将两个时间间隔,换算成秒,取整输出
int AimCompareCurrentToSecond = ((int)((aimTime - currentTime).TotalSeconds));
waitSecond = AimCompareCurrentToSecond + 1;
//MessageBox.Show(AimCompareCurrentToSecond.ToString());
}
//设置自动关机
ShutdownPC(false, (uint)waitSecond);
label_current.Text = "已设置自动关机";
button_cancel.Enabled = true;
button_sure.Enabled = false;
}
/// <summary>
/// 取消关机按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_cancel_Click(object sender, EventArgs e)
{
ShutdownPC(true, 0);//true取消自动关机,0关机延迟时间
label_current.Text = "无自动关机";
button_cancel.Enabled = false;
button_sure.Enabled = true;
}
}
}