C# 定時關機程式碼
阿新 • • 發佈:2020-12-29
c#定時關機程式碼
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(); } } }