1. 程式人生 > >實現註銷 關機 重啟計算機

實現註銷 關機 重啟計算機

NPU -s cts send charset lse tar eve 程序

實現效果:  

  技術分享圖片

知識運用:

  方案1:

    ExitWindowsEx函數 //主要用來退出Windows操作系統 並用特定的選項重新啟動

    uFlags:要執行的操作  dwReserved:保留值 一般為0

    技術分享圖片

  方案2:

    調用DOS命令 需使用Process類  (常用屬性)

    技術分享圖片

                     (常用方法)

    技術分享圖片

實現代碼:

      
        [DllImport("user32.dll", EntryPoint = "ExitWindowsEx", CharSet = CharSet.Ansi)]
        private static extern int ExitWindowsEx(int uFlags,int dwReserved);
        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";           //啟動CMD命令
            myProcess.StartInfo.UseShellExecute = false;        //是否使用系統外殼程序啟動進程
            myProcess.StartInfo.RedirectStandardOutput = true;  //是否寫入流
            myProcess.StartInfo.RedirectStandardInput = true;   //是否從流中讀取
            myProcess.StartInfo.RedirectStandardError = true;   //是否將錯誤信息寫入流
            myProcess.StartInfo.CreateNoWindow = true;          //是否在新窗口中啟動進程
            myProcess.Start();                                  //啟動進程
            myProcess.StandardInput.WriteLine("shutdonw -s -t 0");  //執行關機命令
        }

        private void button3_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
            myProcess.StartInfo.FileName = "cmd.exe";           //啟動CMD命令
            myProcess.StartInfo.UseShellExecute = false;        //是否使用系統外殼程序啟動進程
            myProcess.StartInfo.RedirectStandardOutput = true;  //是否寫入流
            myProcess.StartInfo.RedirectStandardInput = true;   //是否從流中讀取
            myProcess.StartInfo.RedirectStandardError = true;   //是否將錯誤信息寫入流
            myProcess.StartInfo.CreateNoWindow = true;          //是否在新窗口中啟動進程
            myProcess.Start();                                  //啟動進程
            myProcess.StandardInput.WriteLine("shutdonw -r -t 0");  //執行重啟命令
        }

  

實現註銷 關機 重啟計算機