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