C# 定时关机代码
c#定时关机代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace PCShutDown { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void Form2_Load( object sender, EventArgs e) { } string cmd = @"c:\Windows\System32\shutdown.exe" ; /// <summary> /// 锁定电脑 /// </summary> [DllImport( "User32.DLL" )] public static extern void LockWorkStation(); private void btn_Lock_Click( object sender, EventArgs e) { if (MessageBox.Show( "确定要锁定电脑吗?" , "提示" , MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { LockWorkStation(); } } /// <summary> /// 重启电脑 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_Reboot_Click( object sender, EventArgs e) { if (MessageBox.Show( "确定要重记电脑吗?" , "提示" , MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { System.Diagnostics.Process.Start( "shutdown.exe" , " -r -t 60" ); } } /// <summary> /// 关机 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_Shutdown_Click( object sender, EventArgs e) { if (MessageBox.Show( "确定要关机吗?" , "提示" , MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { System.Diagnostics.Process.Start( "shutdown.exe" , " -s -t 60" ); } } /// <summary> /// 取消操作 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_Cancel_Click( object sender, EventArgs e) { if (MessageBox.Show( "确定要取消吗?" , "提示" , MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { System.Diagnostics.Process.Start( "shutdown.exe" , " -a" ); textBox1.AppendText( "提示: 操作时间:" + DateTime.Now.ToString() + " 任务已取消" + "\r\n" ); } } /// <summary> /// 休眠 /// </summary> /// <param name="hiberate"></param> /// <param name="forceCritical"></param> /// <param name="disableWakeEvent"></param> /// <returns></returns> [DllImport( "PowrProf.dll" , CharSet = CharSet.Auto, ExactSpelling = true )] public static extern bool SetSuspendState( bool hiberate, bool forceCritical, bool disableWakeEvent); private void button2_Click( object sender, EventArgs e) { if (MessageBox.Show( "确定进入睡眠模式吗?" , "提示" , MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { SetSuspendState( true , true , true ); } showTime.Text = GetStartTime().ToString(); } } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 超详细,DeepSeek 接入PyCharm实现AI编程!(支持本地部署DeepSeek及官方Dee
· 用 DeepSeek 给对象做个网站,她一定感动坏了
· .NET 8.0 + Linux 香橙派,实现高效的 IoT 数据采集与控制解决方案
· .NET中 泛型 + 依赖注入 的实现与应用