Introduction
This article is about locking, logging off, rebooting, shutting down, hibernating and putting the system on stand by mode in .Net. Here we are going to use both unmanaged code and .Net framework for these functions.
Getting Started
Let us start by creating a windows application. To our newly created form, add seven buttons entitled btnLockComp, btnLogOff, btnReboot, btnShutdown, btnForceLogOff, btnHibernate, btnStandby.

Lock Workstation
Let us start with locking the workstation, which is supposed to lock the current user session. We will call a windows API for doing this. For calling an un-managed piece of code, we need to add the System.Runtime.InteropServices namespace to the using directive.
using System.Runtime.InteropServices;
Now we are ready to import the windows API library and define the function that we intend to use. The function to lock the workstation resides in the user32.dll library. And the function for locking the desktop is LockWorkStation. The following statements should be added to the class to import the library.
[DllImport("user32.dll")]
public static extern void LockWorkStation();
Next step is to double click the btnLockComp button to create a click event handler and call the LockWorkStation API to lock the workstation.
LockWorkStation();
Log Off
For logging off we are going to use an unmanaged API function called ExitWindowsEx(). This function accepts two arguments, one for flag(logoff, shutdown, reboot, etc.,) and the other for reason for this action(maintenance, software update, etc...). Now import the user32.dll once again, this time so we can use the ExitWindowsEx() function, as shown below:
[DllImport("user32.dll")]
public static extern int ExitWindowsEx(int uFlags, int dwReason);
Double click the log off button and add the following function call to the event. Flag 0 indicates logoff,
ExitWindowsEx(0, 0);
To force processes to terminate while logging off, change the flag to 4 in the function as below:
ExitWindowsEx(4, 0);
Reboot
To reboot we are going to use the same function ExitWindowsEx but with a different flag. Add the following code to the click event handler of the reboot button.
ExitWindowsEx(2, 0);
Shutdown
Now add the following code to the button Shutdown's click event handler.
ExitWindowsEx(1, 0);
Hibernate and Standby
To put the system in hibernate and standby modes, we are going to use Application class's SetSuspendState method. There are three arguments for this function, power state, force and disable wake event. The first argument, power state is where we mention the state of the system (hibernate/suspend).
// Hibernate
Application.SetSuspendState(PowerState.Hibernate, true, true);
// Standby
Application.SetSuspendState(PowerState.Suspend true, true);
Conclusion
Please note that these methods were only tested in Windows XP Home and Professional environments, however they should work on any systems that have .NET Framework installed.
Thats all folks!

sahar shahPosted Jun 8, 2019, 2:57 AM
Sir need the same things in visual basic can you send it [email protected]
anthony rajaPosted Jan 9, 2011, 4:57 AM
hi Thiagu i m new for .net c# can u help me how to take a backup and restore sql 2005 data in windows form,i m frm chennai
Madhuri PatilPosted Jan 5, 2010, 12:40 AM
i have created a service, i want to logoff from current user and login again with different user. please help me
alen caljkusicPosted Jul 12, 2009, 10:07 PM
shutdown and reboot don't work for me
sasi kumarPosted Apr 5, 2009, 2:18 PM
hai thiyagu it's me shasi frm madurai... i wan to get skilled in C#, can u help for that , if u like mail me on "[email protected]" , plz favour on me....
srinivas vasuPosted Dec 30, 2008, 12:29 AM
hi friends can anyone tell me where stand by time stores in registry?
vinod NairPosted Oct 31, 2008, 6:15 PM
Thanks that great....
manoj malviyaPosted Mar 3, 2008, 8:32 PM
Please share the code to do that
AakashPosted Mar 1, 2008, 5:48 PM
Nice article. It was very helpful and written very nicely in detail. I was working with WPF application and just want to make a note, that if you are working on a WPF application, you will not see "SetSuspendState(..)" method in Application class. For that to work, you need to add reference to Windows Forms assembly. Cheers, Aakash
TomaszPosted Jan 13, 2008, 7:37 AM
Very good text
Murad BASDAGPosted Dec 3, 2007, 10:49 AM
I Tried these kodes in my computers (Windows XP AND Vista) but they didn't work. Please help me because i need it
vivek talwarPosted Jul 29, 2007, 3:29 AM
Application.SetSuspendState(PowerState.Suspend true, true); this code have error the correct code will be: Application.SetSuspendState(PowerState.Suspend ,true, true);
Mark DeePosted Jul 10, 2007, 8:38 PM
good day. i hope you could help me out. i read you article regarding the utility to lock, shutdown, restart, etc. a computer using a windows application written in c#. it was a good help for me, however i was thinking to develop one the could remotely shutdown or restart a computer over the network using its IP address or computer name. thanks a lot. hope to hear from you soon..please reach me out through my e-mail which is [email protected]
Guru GowdaPosted Jun 22, 2007, 8:44 AM
If i copy n paste crystal report from one system to other the font n page size will change pls help... Guru
Mohana MadhuriPosted May 22, 2007, 5:20 AM
How to implement the RemoteDesktopConnection using a Windows Appplication in C#.net?
Asif JilaniPosted Feb 14, 2007, 4:02 AM
I have designed a simple, one page web form in ASP .Net that contains a Crystal reports Viewer control. The page loads, the viewer shows the report and all of the data. However, if I click on any button from the CR toolbar, the page is re-posted and the viewer disappears from the page.
Asif JilaniPosted Feb 14, 2007, 4:01 AM
I have designed a simple, one page web form in ASP .Net that contains a Crystal reports Viewer control. The page loads, the viewer shows the report and all of the data. However, if I click on any button from the CR toolbar, the page is re-posted and the viewer disappears from the page.
Thiagarajan AlagarsamyPosted Feb 14, 2007, 2:24 AM
Thanks simonhazelgrove. I will correct it. And thats a good catch.
Simon HazelgroveeditedPosted Feb 13, 2007, 5:14 PMEdited Feb 13, 2007, 5:15 PM
I believe there is a typo in this articles tag line :)