using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace AutoCloseComputer
{
public partial class Form1 : Form
{
public Form1()
{ InitializeComponent(); //设置默认时间为当前时间
nud_hour.Value = Convert.ToDecimal(DateTime.Now.Hour);
nud_min.Value = Convert.ToDecimal(DateTime.Now.Minute); txt_sencods.Text = ""; //设置默认时间为10*60秒 } /// <summary>
/// 启动定时关机程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCloseComputer_Click(object sender, EventArgs e)
{
lab_result.Text = ""; string str = txt_sencods.Text.Trim();
if (string.IsNullOrEmpty(str))
{
lab_result.Text = "时间不能为空";
return;
} int seconds = ;
if (int.TryParse(str, out seconds))
{
OperateComputer("shutdown -s -t " + seconds);
}
else
{
lab_result.Text = "时间格式只能输入数字";
}
} /// <summary>
/// 取消关机命令
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCancelClose_Click(object sender, EventArgs e)
{
OperateComputer("shutdown -a");
} /// <summary>
/// 操作电脑命令
/// </summary>
/// <param name="command"></param>
private void OperateComputer(string command)
{
var startInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe");
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.CreateNoWindow = true;
var myProcess = new System.Diagnostics.Process();
myProcess.StartInfo = startInfo;
myProcess.Start();
myProcess.StandardInput.WriteLine(command);
} /// <summary>
/// 指定时间关机
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnTimeClose_Click(object sender, EventArgs e)
{
int hour = Convert.ToInt32(nud_hour.Value);
int min = Convert.ToInt32(nud_min.Value); DateTime closedt = DateTime.Now; //当时间小于当前时认为是第二天的时间
if(hour<DateTime.Now.Hour)
{
string strdt = DateTime.Now.AddDays(1D).ToString("yyyy-MM-dd") + " " + hour.ToString().PadLeft(,'') + ":" + min.ToString().PadLeft(,'');
closedt = Convert.ToDateTime(strdt); }else
{
string strdt = DateTime.Now.ToString("yyyy-MM-dd") + " " + hour.ToString().PadLeft(, '') + ":" + min.ToString().PadLeft(, '');
closedt = Convert.ToDateTime(strdt);
}
//计算出时间差
int total = Convert.ToInt32((closedt - DateTime.Now).TotalSeconds);
OperateComputer("shutdown -s -t " + total); //一下命令没有效果
//string command = "at " + hour + ":" + min + " shutdown -s";
//OperateComputer(command); } }
}

此程序是VS2013编写的。

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

最新文章

  1. 第 2 章 VBScript基本概念
  2. CentOS7安装MySQL、Redis、RabbitMQ
  3. C# MDI子窗体互相操作
  4. 在RedHat上安装gcc,java 和 eclipse-rcp
  5. 安装SQL SERVER开启SA用户登录的方法
  6. [转]javascript 快速隐藏/显示万行表格列的方法
  7. Java UDP网络编程 - 最简单示例
  8. pku,杨建武:文本挖掘技术
  9. 20151212Jquery 工具函数代码备份
  10. Silverlight学习(四) domainservice动态多条件查询
  11. leetcode:linked_list_cycle_II
  12. css实现中文换行,英文换行,超出省略
  13. C语言输出
  14. cat &lt;&lt;EOF
  15. 找出程序GasMileage中的哪一行与下列叙述相对应:
  16. Confluence 6 找到你的支持识别代码(SEN)
  17. js传递数组
  18. Linux sed命令 以行为单位编辑文本,或替换文本中的文字
  19. Keras 资源
  20. J - MANAGER(2.4.5)

热门文章

  1. [深度分析] Python Web 开发框架 Bottle
  2. web前端 3大储存 Cookie、localStorge、sessionStorage
  3. (转)C# 控制蜂鸣器发声
  4. protocol buf安装
  5. centos7 图形化界面
  6. truncate(can)
  7. 为DOM节点添加或者删除class
  8. fabric镜像安装脚本分析
  9. 55.纯 CSS 创作一个太阳、地球、月亮的运转模型
  10. Linux 配置开机自启 和 修改环境变量