admin管理员组文章数量:1609986
由于电脑开了共享,下班后有同事需要使用共享的,就不能关机,而且每次都手动cmd打命令很烦,故做一个小软件,给自己用。
核心还是这两句cmd的命令
延迟关机
shutdown -s -t 3600
取消关机
shutdown -a
主要方法如下,使用C#调用CMD并发送命令
private static void ShutdownPC(bool isCancel, uint interval) {
Process proc = new Process();
proc.StartInfo.FileName = "cmd.exe"; // 启动命令行程序
proc.StartInfo.UseShellExecute = false; // 不使用Shell来执行,用程序来执行
proc.StartInfo.RedirectStandardError = true; // 重定向标准输入输出
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.CreateNoWindow = true; // 执行时不创建新窗口
proc.Start();
string commandLine;
if (isCancel)
commandLine = @"shutdown /a";
else
commandLine = @"shutdown /s /t " + interval.ToString();
proc.StandardInput.WriteLine(commandLine);
}
下面贴出完整代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 自动关机 {
public partial class Form1 : Form {
//判断,当前使用的时间控件,还是秒数输入框
private bool isUsingSecondTextbox = false;
public Form1() {
InitializeComponent();
}
private static void ShutdownPC(bool isCancel, uint interval) {
Process proc = new Process();
proc.StartInfo.FileName = "cmd.exe"; // 启动命令行程序
proc.StartInfo.UseShellExecute = false; // 不使用Shell来执行,用程序来执行
proc.StartInfo.RedirectStandardError = true; // 重定向标准输入输出
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.CreateNoWindow = true; // 执行时不创建新窗口
proc.Start();
string commandLine;
if (isCancel)
commandLine = @"shutdown /a";
else
commandLine = @"shutdown /s /t " + interval.ToString();
proc.StandardInput.WriteLine(commandLine);
}
/// <summary>
/// 确定按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_sure_Click(object sender, EventArgs e) {
//延迟关机,等待的时间
int waitSecond = 0;
//使用秒数框输入
if (isUsingSecondTextbox) {
waitSecond = int.Parse(textBox_second.Text);
}
//使用时间输入
else {
DateTime currentTime = DateTime.Now.ToLocalTime();//获取当前时间
DateTime aimTime = dateTimePicker_aim.Value;//获取目标时间
//比较两个时间。关机时间也在当前时间之后
if (DateTime.Compare(currentTime, aimTime) >= 0) {
MessageBox.Show("目标时间小于当前时间");
return;
}
else {
//将两个时间间隔,换算成秒,取整输出
int AimCompareCurrentToSecond = ((int)((aimTime - currentTime).TotalSeconds));
waitSecond = AimCompareCurrentToSecond + 1;
//MessageBox.Show(AimCompareCurrentToSecond.ToString());
}
}
//设置自动关机
ShutdownPC(false, (uint)waitSecond);
label_current.Text = "已设置自动关机";
button_cancel.Enabled = true;
button_sure.Enabled = false;
}
/// <summary>
/// 取消关机按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_cancel_Click(object sender, EventArgs e) {
ShutdownPC(true, 0);
label_current.Text = "无自动关机";
button_cancel.Enabled = false;
button_sure.Enabled = true;
}
/// <summary>
/// 切换【使用时间控件】还是【直接输入秒数】进行关机时间的设定
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void checkBox1_CheckedChanged(object sender, EventArgs e) {
isUsingSecondTextbox = checkBox1.Checked;
dateTimePicker_aim.Visible = !isUsingSecondTextbox;
textBox_second.Visible = isUsingSecondTextbox;
}
}
}
如果只需要一个类似功能的小软件,可以在下面连接下载
https://download.csdn/download/u011643463/18437682
参考文章:
实现一个自动关机的小程序 https://wwwblogs/moonz-wu/archive/2008/01/16/1041749.html
DateTime相关
日期控件:https://blog.csdn/zs1342084776/article/details/91524422
时间比较:https://wwwblogs/rwh871212/p/6999704.html
Form相关
禁调大小:https://wwwblogs/shuang121/archive/2013/06/10/3130541.html
修改图标:https://jingyan.baidu/article/915fc4149e399a51394b2021.html
版权声明:本文标题:【免费】C# 窗口程序 制作简易定时关机 自动关机 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1728588848a1165062.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论