FOLLOW US
softpcapps Software CODE HELP BLOG

Shareware and free Open Source Windows Software Applications and free Online Tools

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