码迷,mamicode.com
首页 > Windows程序 > 详细

C# 简单的定时关机

时间:2016-07-05 18:59:09      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 
 10 namespace AutoCloseComputer
 11 {
 12     public partial class Form1 : Form
 13     {
 14         public Form1()
 15         { 
 16 
 17             InitializeComponent();
 18             
 19             //设置默认时间为当前时间
 20             nud_hour.Value = Convert.ToDecimal(DateTime.Now.Hour);
 21             nud_min.Value = Convert.ToDecimal(DateTime.Now.Minute);
 22 
 23             txt_sencods.Text = "600";  //设置默认时间为10*60秒
 24             
 25         }
 26 
 27         /// <summary>
 28         /// 启动定时关机程序
 29         /// </summary>
 30         /// <param name="sender"></param>
 31         /// <param name="e"></param>
 32         private void btnCloseComputer_Click(object sender, EventArgs e)
 33         {
 34             lab_result.Text = "";
 35 
 36             string str = txt_sencods.Text.Trim();
 37             if (string.IsNullOrEmpty(str))
 38             {
 39                 lab_result.Text = "时间不能为空";
 40                 return;
 41             }
 42 
 43             int seconds = 0;
 44             if (int.TryParse(str, out seconds))
 45             { 
 46                 OperateComputer("shutdown -s -t " + seconds); 
 47             }
 48             else
 49             {
 50                 lab_result.Text = "时间格式只能输入数字";
 51             }
 52         }
 53 
 54         /// <summary>
 55         /// 取消关机命令
 56         /// </summary>
 57         /// <param name="sender"></param>
 58         /// <param name="e"></param>
 59         private void btnCancelClose_Click(object sender, EventArgs e)
 60         {
 61             OperateComputer("shutdown -a"); 
 62         }
 63 
 64 
 65         /// <summary>
 66         /// 操作电脑命令
 67         /// </summary>
 68         /// <param name="command"></param>
 69         private void OperateComputer(string command)
 70         {
 71             var startInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe");
 72             startInfo.UseShellExecute = false;
 73             startInfo.RedirectStandardInput = true;
 74             startInfo.RedirectStandardOutput = true;
 75             startInfo.RedirectStandardError = true;
 76             startInfo.CreateNoWindow = true;
 77             var myProcess = new System.Diagnostics.Process();
 78             myProcess.StartInfo = startInfo;
 79             myProcess.Start();
 80             myProcess.StandardInput.WriteLine(command);
 81         }
 82 
 83 
 84         /// <summary>
 85         /// 指定时间关机
 86         /// </summary>
 87         /// <param name="sender"></param>
 88         /// <param name="e"></param>
 89         private void btnTimeClose_Click(object sender, EventArgs e)
 90         {
 91             int hour = Convert.ToInt32(nud_hour.Value);
 92             int min = Convert.ToInt32(nud_min.Value);
 93 
 94             DateTime closedt = DateTime.Now;
 95 
 96             //当时间小于当前时认为是第二天的时间
 97             if(hour<DateTime.Now.Hour)
 98             {
 99                 string strdt = DateTime.Now.AddDays(1D).ToString("yyyy-MM-dd") + " " + hour.ToString().PadLeft(2,0) + ":" + min.ToString().PadLeft(2,0);
100                 closedt = Convert.ToDateTime(strdt);
101 
102             }else
103             {
104                 string strdt = DateTime.Now.ToString("yyyy-MM-dd") + " " + hour.ToString().PadLeft(2, 0) + ":" + min.ToString().PadLeft(2, 0);
105                 closedt = Convert.ToDateTime(strdt);
106             }
107             //计算出时间差
108             int total = Convert.ToInt32((closedt - DateTime.Now).TotalSeconds);
109             OperateComputer("shutdown -s -t " + total);
110 
111             //一下命令没有效果
112             //string command = "at " + hour + ":" + min + " shutdown -s";
113             //OperateComputer(command); 
114 
115         }
116          
117     }
118 }

 

技术分享

 

此程序是VS2013编写的。

提供程序的下载链接:https://pan.baidu.com/s/1slpGGsl

 

C# 简单的定时关机

标签:

原文地址:http://www.cnblogs.com/wind-wang/p/5644445.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!