源码如下:

using System;

using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace 关机重启

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        [DllImport("user32.dll", EntryPoint = "ExitWindowsEx", CharSet = CharSet.Ansi)]

        private static extern int ExitWindowsEx(int uFlags,int dwReserved);

        /// <summary>

        /// 注销计算机

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void button1_Click(object sender, EventArgs e)

        {

            ExitWindowsEx(0, 0);

        }

        /// <summary>

        /// 关闭计算机

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void button2_Click(object sender, EventArgs e)

        {

            System.Diagnostics.Process myProcess = new System.Diagnostics.Process();

            myProcess.StartInfo.FileName = "cmd.exe";

            myProcess.StartInfo.UseShellExecute = false;

            myProcess.StartInfo.RedirectStandardInput = true;

            myProcess.StartInfo.RedirectStandardOutput = true;

            myProcess.StartInfo.RedirectStandardError = true;

            myProcess.Start();

            myProcess.StandardInput.WriteLine("shutdown -s -t 0");

        }

        /// <summary>

        /// 重启计算机

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void button3_Click(object sender, EventArgs e)

        {

            System.Diagnostics.Process myProcess = new System.Diagnostics.Process();

            myProcess.StartInfo.FileName = "cmd.exe";

            myProcess.StartInfo.UseShellExecute = false;

            myProcess.StartInfo.RedirectStandardInput = true;

            myProcess.StartInfo.RedirectStandardOutput = true;

            myProcess.StartInfo.RedirectStandardError = true;

            myProcess.StartInfo.CreateNoWindow = true;

            myProcess.Start();

            myProcess.StandardInput.WriteLine("shutdown -r -t 0");

        }

    }

}

以上是打开cmd.exe,然后写命令来实现关机等功能