在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
昨天晚上想要让电脑在百度云上下载很多东西,但是找不到百度云上有下载完后自动关机的选择项,感觉很懊恼。只能自己用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 |
请发表评论