Shoutdown/Logoff/Hibernate/Stand By - Use CODE to do 'em all

Step 1: Add the following line to use the namespace. This is necessary as we need to use the User32.dll with DLLImport attribute.

using System.Runtime.InteropServices;

Step 2: Now add the below line to your code to use the methods from the User32.dll.

[DllImport("user32.dll")]
public static extern void LockWorkStation();
[DllImport("user32.dll")]
public static extern int ExitWindowsEx(int uFlags, int dwReason);

Step 3: Once you complete the above two steps, you can now call the methods on button click or after some or with some particular event or the way you like.

LockWorkStation(); //Locks the workstation, Equivalent to WIN+L
ExitWindowsEx(0, 0)//Shutdowns the machine 
Application.SetSuspendState(PowerState.Suspend, true, true)  //Put the machine in stand-by mode
Application.SetSuspendState(PowerState.Hibernate, true, true); // Hibernates the system
comments powered by Disqus