C#:Process控制電腦 關機,重啟,登出
阿新 • • 發佈:2018-12-27
1.介面
窗體中還有一個定時器 timer1 ,其有一個定時事件 timer1_Tick
2.程式碼
using System; using System.Diagnostics; using System.Windows.Forms; namespace 定時關機工具 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { timer1.Start(); //數字選擇框不可用,除非選擇了計劃關機CheckBox numericUpDown1.Enabled = false; numericUpDown2.Enabled = false; numericUpDown3.Enabled = false; } //定時器出發事件 private void timer1_Tick(object sender, EventArgs e) { label1.Text = DateTime.Now.ToString(); } private void button1_Click(object sender, EventArgs e) { if (checkBox1.Checked) { if (MessageBox.Show("將要設定計劃關機,是否確認操作?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK) { decimal decl = numericUpDown1.Value * 3600 + numericUpDown2.Value * 60 + numericUpDown3.Value; string str = decl.ToString(); // -t xx 表示多少秒之後 Process.Start("shutdown.exe", "-s -t " + str); } } else { if (MessageBox.Show("是否確認關機?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK) { Process.Start("shutdown.exe", "-s"); } } } // -t 10 表示10秒之後 private void button2_Click(object sender, EventArgs e) { if (MessageBox.Show("是否確認重啟?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK) { Process.Start("shutdown.exe", "-r"); Process.Start("shutdown.exe", "-r -t 10"); } } private void button3_Click(object sender, EventArgs e) { if (MessageBox.Show("是否確認登出?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK) Process.Start("shutdown.exe", "-l"); } private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (checkBox1.Checked) { numericUpDown1.Enabled = true; numericUpDown2.Enabled = true; numericUpDown3.Enabled = true; button1.Text = "計劃關機"; } else { numericUpDown1.Enabled = false; numericUpDown2.Enabled = false; numericUpDown3.Enabled = false; button1.Text = "關機"; } } } }