2011-01-30 9 views
0

C#プログラムを使用してコンピュータをシャットダウン/休止/再起動/スリープさせることはできますか?シャットダウン/再起動のシステム利用へC#shutdown/hibernate/sleep

答えて

3

-

using System.Management; 

    void Shutdown() 
    { 
     ManagementBaseObject mboShutdown = null; 
     ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem"); 
     mcWin32.Get(); 

     // You can't shutdown without security privileges 
     mcWin32.Scope.Options.EnablePrivileges = true; 
     ManagementBaseObject mboShutdownParams = 
       mcWin32.GetMethodParameters("Win32Shutdown"); 

     // **Flag 1 means we want to shut down the system. Use "2" to reboot**. 
     mboShutdownParams["Flags"] = "1"; 
     mboShutdownParams["Reserved"] = "0"; 
     foreach (ManagementObject manObj in mcWin32.GetInstances()) 
     { 
      mboShutdown = manObj.InvokeMethod("Win32Shutdown", 
              mboShutdownParams, null); 
     } 
    } 

more here

休止状態の場合とでスタンド - あなたは.NET 2.0を使用している場合、あなただけの Application.SetSuspendStateを呼び出すことができます。

Check out the documentation at:

1
Process.Start("shutdown", "/s /t 0"); // shutdown 
Process.Start("shutdown", "/r /t 0"); // shutdown and restart 
Process.Start("shutdown", "/h /f"); // hibernate