FOLLOW US
4dots Software CODE BLOG

How to shutdown, hibernate, restart, sleep, logoff, lock computer programmatically

To shutdown, hibernate, restart, sleep, logoff, lock computer programmatically use the following class :
	

	public class ShutdownHelper
    {
		[DllImport("user32")]
        public static extern void LockWorkStation();
        [DllImport("user32")]
        public static extern bool ExitWindowsEx(uint uFlags, uint dwReason);
		
        public static void Hibernate()
        {
            Application.SetSuspendState(PowerState.Hibernate, true, true);
        }

        public static void Sleep()
        {
            Application.SetSuspendState(PowerState.Suspend, true, true);
        }        
        
        public static void Shutdown()
        {
            Process.Start("shutdown","/s /t 0");
        }

        public static void Restart()
        {
            Process.Start("shutdown", "/r /t 0");
        }

        public static void Logoff()
        {
            ExitWindowsEx(0, 0);
  
        }
		
        public static void Lock()
        {
            LockWorkStation();
        }
    }


 

Please Donate!

Please Share!