using System;
using System.IO;
using System.Windows.Forms;
namespace AutoCloseComputer
{
public partial class Form1 : Form
{
public string LogFile { get; private set; }
public Form1()
{
InitializeComponent();
//设置默认时间为当前时间
nud_hour.Value = Convert.ToDecimal(DateTime.Now.Hour);
nud_min.Value = Convert.ToDecimal(DateTime.Now.Minute);
txt_sencods.Text = "600"; //设置默认时间为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 = 0;//如果输入为0
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(2, '0') + ":" + min.ToString().PadLeft(2, '0');
closedt = Convert.ToDateTime(strdt);//如果时间小于本地时间则会提示在明天的这个时间关机
}
else
{
string strdt = DateTime.Now.ToString("yyyy-MM-dd") + " " + hour.ToString().PadLeft(2, '0') + ":" + min.ToString().PadLeft(2, '0');
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);
public void CheckLog(string Log)
{
if (File.Exists(LogFile))
{
WriteLog(Log);
}
else
{
CreateLog();
WriteLog(Log);
}
}
private void CreateLog()
{
StreamWriter SW;
SW = File.CreateText(LogFile);
SW.WriteLine("Log created at: " +
DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss"));
SW.Close();
}
private void WriteLog(string Log)
{
using (StreamWriter SW = File.AppendText(LogFile))
{
SW.WriteLine(Log);
SW.Close();
}
}
}
}
using System.IO;
using System.Windows.Forms;
namespace AutoCloseComputer
{
public partial class Form1 : Form
{
public string LogFile { get; private set; }
public Form1()
{
InitializeComponent();
//设置默认时间为当前时间
nud_hour.Value = Convert.ToDecimal(DateTime.Now.Hour);
nud_min.Value = Convert.ToDecimal(DateTime.Now.Minute);
txt_sencods.Text = "600"; //设置默认时间为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 = 0;//如果输入为0
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(2, '0') + ":" + min.ToString().PadLeft(2, '0');
closedt = Convert.ToDateTime(strdt);//如果时间小于本地时间则会提示在明天的这个时间关机
}
else
{
string strdt = DateTime.Now.ToString("yyyy-MM-dd") + " " + hour.ToString().PadLeft(2, '0') + ":" + min.ToString().PadLeft(2, '0');
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);
public void CheckLog(string Log)
{
if (File.Exists(LogFile))
{
WriteLog(Log);
}
else
{
CreateLog();
WriteLog(Log);
}
}
private void CreateLog()
{
StreamWriter SW;
SW = File.CreateText(LogFile);
SW.WriteLine("Log created at: " +
DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss"));
SW.Close();
}
private void WriteLog(string Log)
{
using (StreamWriter SW = File.AppendText(LogFile))
{
SW.WriteLine(Log);
SW.Close();
}
}
}
}