定时关机软件

我们有时候会在睡觉前挂机电脑干一些事,比如下片等,会发现这部电影会在两个小时内下完,但是机子之后还开着的会浪费资源和损坏机器,所以定时关机很重要。我发现,网上下的一些软件竟然都带有广告,而且杂乱,不符合绿色版。我自己就写了一个绿色简单版的,只有定时关机这一种功能。效果图如下:

我知道可以运行cmd,用shutdown命令可以进行定时关机,所以我的想法是用C#调用cmd命令,从而达到定时关机的目的。

C#调用cmd命令代码为:

 1 //定时关机方法
 2         private void shutdown()
 3         {
 4             int h = int.Parse(textBox1.Text);
 5             int m = int.Parse(textBox2.Text);
 6             int num = h * 3600 + m * 60;
 7             Process myProcess = new Process();
 8             myProcess.StartInfo.FileName = "cmd.exe";//设定程序名
 9             myProcess.StartInfo.UseShellExecute = false;//关闭Shell的使用
10             myProcess.StartInfo.RedirectStandardInput = true;//重定向标准输入
11             myProcess.StartInfo.RedirectStandardOutput = true;//重定向标准输出
12             myProcess.StartInfo.RedirectStandardError = true;//重定向错误输出
13             myProcess.StartInfo.CreateNoWindow = true;//设置不显示窗口
14             myProcess.Start();       //启动进程
15             //在wpf中message返回值是MessageBoxResult
16             MessageBoxResult dr =System.Windows.MessageBox.Show("此机子将在" + h + "小时" + m + "分钟后关机"
17                 , "定时关机",MessageBoxButton.OKCancel);
18             if (dr == MessageBoxResult.OK)
19             {
20                 myProcess.StandardInput.WriteLine("shutdown -s -t " + num);
21             }
22             else
23             {
24                 return;
25             }
26         }
调用cmd命令

显示当时时间很简单,运用DispatchTimer类

 1 private void Window_Loaded(object sender, RoutedEventArgs e)
 2         {
 3             DispatcherTimer time = new DispatcherTimer();
 4             time.Interval = new TimeSpan(0, 0, 1);
 5             time.Tick+=new EventHandler(time_Tick);
 6             time.Start();
 7         }
 8         void time_Tick(object sender,EventArgs e)
 9         {
10             label1.Content = "当前时间为:"+DateTime.Now.ToLongTimeString();
11         }

当然,输入textbox中的只能是整数,所以要进行一些判断。

 1 //自定义一个函数,判断textbox中输入的是不是数字
 2         private bool IsNumber(object obj)
 3         {
 4             bool result = false;
 5             try
 6             {
 7                 string str = obj.ToString();
 8                 double d;
 9                 d = double.Parse(str);
10             }
11             catch
12             {  //parse 函数进行转换,不成功则抛出异常
13                 result = true;
14             }
15             return result;
16 
17         }
18         //定义一个方法,判断textbox中输入内容的是否合法,再在后面加入shutdown方法
19         private void defint()
20         {
21             if (textBox1.Text =="" || textBox2.Text == "")
22             {
23                 System.Windows.MessageBox.Show("请在上面方框中输入数字");
24             }
25             else if (IsNumber(textBox1.Text)||IsNumber(textBox2.Text))
26             {
27                 System.Windows.MessageBox.Show("请输入正确整数,而不是字符");
28             }
29             else if (int.Parse(textBox1.Text) < 0 || int.Parse(textBox2.Text) < 0)
30             {
31                 System.Windows.MessageBox.Show("请输入合理范围内的数字");
32             }
33             else
34             {
35                 shutdown();
36             }
37         } 

总的就这些,不复杂的程序,下面贴上全部的代码,以供参考

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Windows;
  6 using System.Windows.Controls;
  7 using System.Windows.Data;
  8 using System.Windows.Documents;
  9 using System.Windows.Input;
 10 using System.Windows.Media;
 11 using System.Windows.Media.Imaging;
 12 using System.Windows.Navigation;
 13 using System.Windows.Shapes;
 14 using System.Windows.Threading;
 15 using System.Diagnostics;
 16 using System.Drawing;
 17 using System.Windows.Forms;
 18 
 19 namespace 定时关机软件
 20 {
 21     /// <summary>
 22     /// MainWindow.xaml 的交互逻辑
 23     /// </summary>
 24     public partial class MainWindow : Window
 25     {
 26         public MainWindow()
 27         {
 28             InitializeComponent();
 29         }
 30         //显示当前时间
 31         private void Window_Loaded(object sender, RoutedEventArgs e)
 32         {
 33             DispatcherTimer time = new DispatcherTimer();
 34             time.Interval = new TimeSpan(0, 0, 1);
 35             time.Tick+=new EventHandler(time_Tick);
 36             time.Start();
 37         }
 38         void time_Tick(object sender,EventArgs e)
 39         {
 40             label1.Content = "当前时间为:"+DateTime.Now.ToLongTimeString();
 41         }
 42         //自定义一个函数,判断textbox中输入的是不是数字
 43         private bool IsNumber(object obj)
 44         {
 45             bool result = false;
 46             try
 47             {
 48                 string str = obj.ToString();
 49                 double d;
 50                 d = double.Parse(str);
 51             }
 52             catch
 53             {  //parse 函数进行转换,不成功则抛出异常
 54                 result = true;
 55             }
 56             return result;
 57 
 58         }
 59         //定义一个方法,判断textbox中输入内容的是否合法,再在后面加入shutdown方法
 60         private void defint()
 61         {
 62             if (textBox1.Text =="" || textBox2.Text == "")
 63             {
 64                 System.Windows.MessageBox.Show("请在上面方框中输入数字");
 65             }
 66             else if (IsNumber(textBox1.Text)||IsNumber(textBox2.Text))
 67             {
 68                 System.Windows.MessageBox.Show("请输入正确整数,而不是字符");
 69             }
 70             else if (int.Parse(textBox1.Text) < 0 || int.Parse(textBox2.Text) < 0)
 71             {
 72                 System.Windows.MessageBox.Show("请输入合理范围内的数字");
 73             }
 74             else
 75             {
 76                 shutdown();
 77             }
 78         } 
 79         //定时关机方法
 80         private void shutdown()
 81         {
 82             int h = int.Parse(textBox1.Text);
 83             int m = int.Parse(textBox2.Text);
 84             int num = h * 3600 + m * 60;
 85             Process myProcess = new Process();
 86             myProcess.StartInfo.FileName = "cmd.exe";//设定程序名
 87             myProcess.StartInfo.UseShellExecute = false;//关闭Shell的使用
 88             myProcess.StartInfo.RedirectStandardInput = true;//重定向标准输入
 89             myProcess.StartInfo.RedirectStandardOutput = true;//重定向标准输出
 90             myProcess.StartInfo.RedirectStandardError = true;//重定向错误输出
 91             myProcess.StartInfo.CreateNoWindow = true;//设置不显示窗口
 92             myProcess.Start();       //启动进程
 93             //在wpf中message返回值是MessageBoxResult
 94             MessageBoxResult dr =System.Windows.MessageBox.Show("此机子将在" + h + "小时" + m + "分钟后关机"
 95                 , "定时关机",MessageBoxButton.OKCancel);
 96             if (dr == MessageBoxResult.OK)
 97             {
 98                 myProcess.StandardInput.WriteLine("shutdown -s -t " + num);
 99             }
100             else
101             {
102                 return;
103             }
104         }
105         private void button1_Click(object sender, RoutedEventArgs e)
106         {
107             defint();
108         }
109         //取消已经设置的定时关机
110         private void button2_Click(object sender, RoutedEventArgs e)
111         {
112             Process myProcess = new Process();
113             myProcess.StartInfo.FileName = "cmd.exe";//设定程序名
114             myProcess.StartInfo.UseShellExecute = false;//关闭Shell的使用
115             myProcess.StartInfo.RedirectStandardInput = true;//重定向标准输入
116             myProcess.StartInfo.RedirectStandardOutput = true;//重定向标准输出
117             myProcess.StartInfo.RedirectStandardError = true;//重定向错误输出
118             myProcess.StartInfo.CreateNoWindow = true;//设置不显示窗口
119             myProcess.Start();       //启动进程
120             myProcess.StandardInput.WriteLine("shutdown -a ");
121         }
122     }
123 }
全部代码

总结:这个程序是用WPF做的,发现WPF能用的控件太少了,有时候需要自己引用Winform中的控件。比如在这个程序中我需要NotifyIcon这个控件,能让这个程序最小化,但在WPF中没有这个控件,只能百度解决。虽然查到了,但最终还是没能实现,决定暂时跳过。

原文地址:https://www.cnblogs.com/xijianyao/p/3131916.html