天天看點

登出、關閉和重新開機計算機

源碼如下:

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);

        /// 關閉計算機

        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");

        /// 重新開機計算機

        private void button3_Click(object sender, EventArgs e)

            myProcess.StartInfo.CreateNoWindow = true;

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

    }

}

以上是打開cmd.exe,然後寫指令來實作關機等功能

本文轉自蓬萊仙羽51CTO部落格,原文連結:http://blog.51cto.com/dingxiaowei/1366628,如需轉載請自行聯系原作者

繼續閱讀