示例#1
0
        /// <summary>
        /// コンピューターを強制的にシャットダウン・再起動・ログオフ・スタンバイ・休止状態にします。
        /// </summary>
        /// <param name="shutDownSwitch"></param>
        public static void ShutDown(ShutDownSwitch shutDownSwitch)
        {
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                adjustToken();
            }

            EWX ewx = EWX.FORCE;

            switch (shutDownSwitch)
            {
            case ShutDownSwitch.PowerOff:
                ewx |= EWX.POWEROFF;
                break;

            case ShutDownSwitch.Reboot:
                ewx |= EWX.REBOOT;
                break;

            case ShutDownSwitch.LogOff:
                ewx |= EWX.LOGOFF;
                break;

            case ShutDownSwitch.StandBy:
            case ShutDownSwitch.Hibernate:
                bool hibernate = (shutDownSwitch == ShutDownSwitch.Hibernate);
                PowrProf.SetSuspendState(hibernate, true, true);
                return;

            default:
                throw new ArgumentException();
            }

            User32.ExitWindowsEx(ewx, 0);
        }
示例#2
0
 private static bool Suspend(bool hibernate, bool wakeupEventsDisabled)
 {
     try
     {
         using (new PrivilegedCodeBlock(SystemPrivilege.Shutdown))
         {
             if (hibernate)
             {
                 return(PowrProf.IsPwrHibernateAllowed() ? PowrProf.SetSuspendState(true, false, wakeupEventsDisabled) : false);
             }
             else
             {
                 return(PowrProf.IsPwrSuspendAllowed() ? PowrProf.SetSuspendState(false, false, wakeupEventsDisabled) : false);
             }
         }
     }
     catch
     {
         return(false);
     }
 }