C#:Process控制电脑 关机,重启,注销

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 = "关机";
            }
        }
        
    }
}

猜你喜欢

转载自blog.csdn.net/qq_38261174/article/details/85275681