定時倒計時關機的小程序


  昨天晚上想要讓電腦在百度雲上下載很多東西,但是找不到百度雲上有下載完后自動關機的選擇項,感覺很懊惱。只能自己用cmd輸入命令去執行自動關機了,感覺好麻煩。而且這種命令行的執行看起來太不爽了。於是今天在上起床就寫了一個定時關機和倒計時關機的小程序。原理還是使用了cmd命令行。

關機:shutdown -s -t 10表示10s后自動關機

取消:shutdown -a表示取消自動關機

界面如下:

代碼如下:

  1   public partial class Form1 : Form
  2     {
  3         public Form1()
  4         {
  5             InitializeComponent();
  6         }
  7         private bool sureDown = false;
  8         private int countDownTime = 0;
  9         private void timer1_Tick(object sender, EventArgs e)
 10         {
 11             lblDateTime.Text = DateTime.Now.ToString();
 12             //if (sureDown)
 13             //{
 14             //    cboHour.SelectedIndex = Convert.ToInt32(DateTime.Now.Hour);
 15             //    cboMinute.SelectedIndex = Convert.ToInt32(DateTime.Now.Minute); 
 16             //}
 17         }
 18         //選擇定時關機
 19         private void btnSureTime_Click(object sender, EventArgs e)
 20         {
 21             sureDown = true;
 22             cboHour.SelectedIndex = Convert.ToInt32(DateTime.Now.Hour);
 23             cboMinute.SelectedIndex = Convert.ToInt32(DateTime.Now.Minute);
 24         }
 25         //選擇倒計時關機
 26         private void btnCountDown_Click(object sender, EventArgs e)
 27         {
 28             sureDown = false;
 29             cboHour.SelectedIndex =0;
 30             cboMinute.SelectedIndex =30;
 31         }
 32         /// <summary>
 33         /// 執行自動關機,包括定時關機和倒計時關機
 34         /// </summary>
 35         /// <param name="sender"></param>
 36         /// <param name="e"></param>
 37         private void btnDown_Click(object sender, EventArgs e)
 38         {
 39             if (cboHour.Text == "" || cboMinute.Text == "")
 40             {
 41                 MessageBox.Show("請先選擇模式");
 42                 return;
 43             }
 44             if (sureDown)
 45             {
 46                 //定時關機計算時間
 47                 countDownTime = Convert.ToInt32(cboHour.SelectedItem) * 3600 + Convert.ToInt32(cboMinute.SelectedItem) * 60 -Convert.ToInt32(DateTime.Now.Hour)*3600-Convert.ToInt32(DateTime.Now.Minute)*60 -  Convert.ToInt32(DateTime.Now.Second);
 48                 if (countDownTime <= 0)
 49                 {
 50                     countDownTime =24*60*60 +countDownTime;
 51                 }
 52             }
 53             else
 54             {
 55                 //倒計時關機計算時間
 56                 countDownTime = Convert.ToInt32(cboHour.SelectedItem) * 3600 + Convert.ToInt32(cboMinute.SelectedItem) * 60;
 57             }
 58             int[] time = new int[1];
 59             time[0] = countDownTime;
 60             MakeProcess("shutdown -s -t ", time);
 61             Form2 frm = new Form2(countDownTime);
 62             frm.ShowDialog();
 63             //Process cmdP = new Process();
 64             //cmdP.StartInfo.FileName = "cmd.exe";//進程打開文件
 65             //cmdP.StartInfo.UseShellExecute = false;//是否啟動系統外殼
 66             //cmdP.StartInfo.RedirectStandardInput = true;//是否從StandardInout輸入
 67             //cmdP.StartInfo.RedirectStandardOutput = true;
 68             //cmdP.StartInfo.RedirectStandardError = true;
 69             //cmdP.StartInfo.CreateNoWindow = true;//啟動程序時是否顯示窗口
 70             //cmdP.Start();
 71             //cmdP.StandardInput.WriteLine("shutdown -s -t "+countDownTime);
 72             //cmdP.StandardInput.WriteLine("exit");
 73             //cmdP.WaitForExit();
 74             //cmdP.Close();
 75         }
 76         /// <summary>
 77         /// 取消自動關機
 78         /// </summary>
 79         /// <param name="sender"></param>
 80         /// <param name="e"></param>
 81         private void btnRemove_Click(object sender, EventArgs e)
 82         {
 83             MakeProcess("shutdown -a");
 84             //Process cmdP = new Process();
 85             //cmdP.StartInfo.FileName = "cmd.exe";
 86             //cmdP.StartInfo.UseShellExecute = false;
 87             //cmdP.StartInfo.RedirectStandardInput = true;
 88             //cmdP.StartInfo.RedirectStandardOutput = true;
 89             //cmdP.StartInfo.RedirectStandardError = true;
 90             //cmdP.StartInfo.CreateNoWindow = true;
 91             //cmdP.Start();
 92             //cmdP.StandardInput.WriteLine("shutdown -a");
 93             //cmdP.StandardInput.WriteLine("exit");
 94             //cmdP.WaitForExit();
 95             //cmdP.Close();
 96         }
 97         //創建進程用於打開,關閉定時關機
 98         private void MakeProcess(string cmd,params int[] time)
 99         {
100             
101             Process cmdP = new Process();
102             cmdP.StartInfo.FileName = "cmd.exe";//進程打開文件
103             cmdP.StartInfo.UseShellExecute = false;//是否啟動系統外殼
104             cmdP.StartInfo.RedirectStandardInput = true;//是否從StandardInout輸入
105             cmdP.StartInfo.RedirectStandardOutput = true;
106             cmdP.StartInfo.RedirectStandardError = true;
107             cmdP.StartInfo.CreateNoWindow = true;//啟動程序時是否顯示窗口
108             cmdP.Start();
109             if (time.Length<=0)
110             {
111                 cmdP.StandardInput.WriteLine(cmd);
112             }
113             else
114             {
115                 cmdP.StandardInput.WriteLine(cmd + time[0]);
116             }
117             cmdP.StandardInput.WriteLine("exit");
118             cmdP.WaitForExit();
119             cmdP.Close();
120         }
121 
122         private void Form1_Load(object sender, EventArgs e)
123         {
124             cboHour.SelectedIndex = Convert.ToInt32(DateTime.Now.Hour);
125             cboMinute.SelectedIndex = Convert.ToInt32(DateTime.Now.Minute);
126             lblDateTime.Text = DateTime.Now.ToString();
127         }
128     }
 1 設定好時間后的窗體  
 2  public partial class Form2 : Form
 3     {
 4         private int _time;
 5         private int hour = 0;
 6         private int minute = 0;
 7         private int second = 0;
 8 
 9         public int Time
10         {
11             get { return _time; }
12             set { _time = value; }
13         }
14         public Form2(int time)
15         {
16             this.Time = time;
17             hour = time / 3600;
18             minute = (time % 3600) / 60;
19             second = time % 60;
20             InitializeComponent();
21         }
22 
23         private void Form2_Load(object sender, EventArgs e)
24         {
25             lblDateTime.Text = DateTime.Now.ToString();
26             label2.Text = hour + "小時" + minute + "分鍾" + second + "秒后";
27         }
28 
29         private void timer1_Tick(object sender, EventArgs e)
30         {
31             lblDateTime.Text = DateTime.Now.ToString();
32             Time = Time - 1;
33             hour = Time / 3600;
34             minute = (Time % 3600) / 60;
35             second = Time % 60;
36             label2.Text = hour + "小時" + minute + "分鍾" + second + "秒后";
37         }
38 
39         private void MakeProcess(string cmd, params int[] time)
40         {
41 
42             Process cmdP = new Process();
43             cmdP.StartInfo.FileName = "cmd.exe";//進程打開文件
44             cmdP.StartInfo.UseShellExecute = false;//是否啟動系統外殼
45             cmdP.StartInfo.RedirectStandardInput = true;//是否從StandardInout輸入
46             cmdP.StartInfo.RedirectStandardOutput = true;
47             cmdP.StartInfo.RedirectStandardError = true;
48             cmdP.StartInfo.CreateNoWindow = true;//啟動程序時是否顯示窗口
49             cmdP.Start();
50             if (time.Length <= 0)
51             {
52                 cmdP.StandardInput.WriteLine(cmd);
53             }
54             else
55             {
56                 cmdP.StandardInput.WriteLine(cmd + time[0]);
57             }
58             cmdP.StandardInput.WriteLine("exit");
59             cmdP.WaitForExit();
60             cmdP.Close();
61         }
62 
63         private void btnCancle_Click(object sender, EventArgs e)
64         {
65             MakeProcess("shutdown -a");
66             MessageBox.Show("倒計時關機已取消");
67             this.Close();
68         }
69     }

可執行文件:

http://pan.baidu.com/s/1gfr4fcB


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM