学c#已经2个星期了,今天心血来潮想用来做一点东西出来,真好电脑想给电脑整个定时关机功能,不然每次想要定时关机还要自己打开控制台真麻烦。
原理很简单利用控制台的“shutdown.exe -s -t ”命令,来控制定时关机,网上有大佬整的代码,自己分析了一下略加修改,然后整个窗体并稍微做了一下修饰。(开发平台vs2017)

这是程序的窗体,下面我一步步的讲解如何制作。
首先在工具箱窗体拖出3个label组件。

分别布置在如下位置

文本在右下角属性那里修改

拖出一个textbox作为输入文本框,自适应调整大小:

然后是4个button组件,自适应调整大小,文本在红线这里修改

最后我们拖入一个Timer组件,然后双击确定按钮输入如下代码:
private void button1_Click(object sender, EventArgs e)
{
double txtConvertToInt(string txt)
{
double result = 0;
bool f = double.TryParse(txt, out result);
return result;
}
double time =txtConvertToInt(textBox1.Text)*60;
temp = (int)time;
if (textBox1.Text=="")
{
MessageBox.Show("输入不能为空!");
return;
}
else
{
Process p = new Process();
//设置要启动的应用程序
p.StartInfo.FileName = "cmd.exe";
//是否使用操作系统shell启动
p.StartInfo.UseShellExecute = false;
// 接受来自调用程序的输入信息
p.StartInfo.RedirectStandardInput = true;
//输出信息
p.StartInfo.RedirectStandardOutput = true;
// 输出错误
p.StartInfo.RedirectStandardError = true;
//不显示程序窗口
p.StartInfo.CreateNoWindow = true;
//启动程序
p.Start();
//向cmd窗口发送输入信息
p.StandardInput.WriteLine("shutdown.exe -s -t {0}", time);
this.timer1.Enabled = true;
//设置时间间隔(毫秒为单位)
this.timer1.Interval = 1000;
timer1.Start();
button3.Visible = true;
label3.Visible = true;
}
}
然后双击取消关机按钮输入如下代码:
private void button3_Click(object sender, EventArgs e)
{
Process p = new Process();
//设置要启动的应用程序
p.StartInfo.FileName = "cmd.exe";
//是否使用操作系统shell启动
p.StartInfo.UseShellExecute = false;
// 接受来自调用程序的输入信息
p.StartInfo.RedirectStandardInput = true;
//输出信息
p.StartInfo.RedirectStandardOutput = true;
// 输出错误
p.StartInfo.RedirectStandardError = true;
//不显示程序窗口
p.StartInfo.CreateNoWindow = true;
//启动程序
p.Start();//向cmd窗口发送输入信息
p.StandardInput.WriteLine("shutdown /a &exit");
MessageBox.Show("计划中的关机已取消");
button3.Visible = false;
this.timer1.Stop();
this.timer1.Enabled = false;
this.timer1.Dispose();
temp = 0;
label3.Text = "系统将会在 " + temp + " 秒后自动关闭";
}
双击取消按钮输入如下代码,这操作会关闭程序窗口但是不会关闭自动关机的命令。这个是我自己加的感觉不方便的朋友可以按照自己意愿来修改
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("设定的自动关机计划仍在进行.");
this.Close();
}
双击“不,立即重启”添加如下代码
private void button4_Click(object sender, EventArgs e)
{
Process p = new Process();
//设置要启动的应用程序
p.StartInfo.FileName = "cmd.exe";
//是否使用操作系统shell启动
p.StartInfo.UseShellExecute = false;
// 接受来自调用程序的输入信息
p.StartInfo.RedirectStandardInput = true;
//输出信息
p.StartInfo.RedirectStandardOutput = true;
// 输出错误
p.StartInfo.RedirectStandardError = true;
//不显示程序窗口
p.StartInfo.CreateNoWindow = true;
//启动程序
p.Start();//向cmd窗口发送输入信息
p.StandardInput.WriteLine("shutdown /r &exit");
}
双击计时器添加如下代码
private void timer1_Tick(object sender, EventArgs e)
{
temp -= 1;
label3.Text = "系统将会在 " + temp + " 秒后自动关闭";
}
基本上定时关机的小程序已经完成了,可是它仍有不少的bug,比如输入框会接受“@WE¥”之类的输入符号为了限制此类输入我们还要在主程序里面添加如下代码
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 0x20) e.KeyChar = (char)0; //禁止空格键
if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == 0)) return; //处理负数
if (e.KeyChar > 0x20)
{
try
{
double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
}
catch
{
e.KeyChar = (char)0; //处理非法字符
}
}
}
这会限制用户只能输入数字,不过有个小缺点,用户仍然可以输入负数,这里作者留下一个小小的坑,如何解决用户输入负数问题?
整个程序的初始化我们在窗口函数里面输入如下代码:
int temp;
public Form1()
{
InitializeComponent();
button3.Visible = false;
label3.Visible = false;
label3.Text = "系统将会在 "+temp+" 秒后自动关闭";
}
最后我们来运行一下看看

整个项目的源代码如下:
using System;
using System.Windows.Forms;
using System.Diagnostics;
namespace TimerShut
{
public partial class Form1 : Form
{
int temp;
public Form1()
{
InitializeComponent();
button3.Visible = false;
label3.Visible = false;
label3.Text = "系统将会在 "+temp+" 秒后自动关闭";
}
private void Form1_Load(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
}
private void button1_Click(object sender, EventArgs e)
{
double txtConvertToInt(string txt)
{
double result = 0;
bool f = double.TryParse(txt, out result);
return result;
}
double time =txtConvertToInt(textBox1.Text)*60;
temp = (int)time;
if (textBox1.Text=="")
{
MessageBox.Show("输入不能为空!");
return;
}
else
{
Process p = new Process();
//设置要启动的应用程序
p.StartInfo.FileName = "cmd.exe";
//是否使用操作系统shell启动
p.StartInfo.UseShellExecute = false;
// 接受来自调用程序的输入信息
p.StartInfo.RedirectStandardInput = true;
//输出信息
p.StartInfo.RedirectStandardOutput = true;
// 输出错误
p.StartInfo.RedirectStandardError = true;
//不显示程序窗口
p.StartInfo.CreateNoWindow = true;
//启动程序
p.Start();
//向cmd窗口发送输入信息
p.StandardInput.WriteLine("shutdown.exe -s -t {0}", time);
this.timer1.Enabled = true;
//设置时间间隔(毫秒为单位)
this.timer1.Interval = 1000;
timer1.Start();
button3.Visible = true;
label3.Visible = true;
}
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("设定的自动关机计划仍在进行.");
this.Close();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 0x20) e.KeyChar = (char)0; //禁止空格键
if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == 0)) return; //处理负数
if (e.KeyChar > 0x20)
{
try
{
double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
}
catch
{
e.KeyChar = (char)0; //处理非法字符
}
}
}
private void button3_Click(object sender, EventArgs e)
{
Process p = new Process();
//设置要启动的应用程序
p.StartInfo.FileName = "cmd.exe";
//是否使用操作系统shell启动
p.StartInfo.UseShellExecute = false;
// 接受来自调用程序的输入信息
p.StartInfo.RedirectStandardInput = true;
//输出信息
p.StartInfo.RedirectStandardOutput = true;
// 输出错误
p.StartInfo.RedirectStandardError = true;
//不显示程序窗口
p.StartInfo.CreateNoWindow = true;
//启动程序
p.Start();//向cmd窗口发送输入信息
p.StandardInput.WriteLine("shutdown /a &exit");
MessageBox.Show("计划中的关机已取消");
button3.Visible = false;
this.timer1.Stop();
this.timer1.Enabled = false;
this.timer1.Dispose();
temp = 0;
label3.Text = "系统将会在 " + temp + " 秒后自动关闭";
}
private void timer1_Tick(object sender, EventArgs e)
{
temp -= 1;
label3.Text = "系统将会在 " + temp + " 秒后自动关闭";
}
private void button4_Click(object sender, EventArgs e)
{
Process p = new Process();
//设置要启动的应用程序
p.StartInfo.FileName = "cmd.exe";
//是否使用操作系统shell启动
p.StartInfo.UseShellExecute = false;
// 接受来自调用程序的输入信息
p.StartInfo.RedirectStandardInput = true;
//输出信息
p.StartInfo.RedirectStandardOutput = true;
// 输出错误
p.StartInfo.RedirectStandardError = true;
//不显示程序窗口
p.StartInfo.CreateNoWindow = true;
//启动程序
p.Start();//向cmd窗口发送输入信息
p.StandardInput.WriteLine("shutdown /r &exit");
}
}
}
第一次写项目有很多问题欢迎各位大佬指导批评。