Click here to Skip to main content
11,412,182 members (63,958 online)
Click here to Skip to main content

Shut Down, Restart, Log off, Lock, Hibernate or Sleep Your Computer in C#

, 1 Dec 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
A tip about how to shut down, restart, log off, lock, hibernate or sleep your computer in C#.


In this tip, I'll tell you how to shut down, restart, log off or lock your computer in C#.

Using the Code

First, add this using namespace statements:

using System.Diagnostics;
using System.Runtime.InteropServices; 

To shut down your computer, use this code:

Process.Start("shutdown","/s /t 0");    // starts the shutdown application 
                                        // the argument /s is to shut down the computer
                                        // the argument /t 0 is to tell the process that 
                                        // the specified operation needs to be completed 
                                        // after 0 seconds

To restart your computer, use this code:

"/r /t 0"); // the argument /r is to restart the computer

To log off, add this extern method to your class:

public static extern bool ExitWindowsEx(uint uFlags, uint dwReason);

Then, to log off, invoke the method:


To lock your computer, add this extern method to your class:

 public static extern void LockWorkStation();

Then, to lock, invoke the method:


To put your computer in Hibernate or Sleep, you need the same DllImport statement for them. Thanks to Virender Prajapati for suggesting to add these!

[DllImport("PowrProf.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern bool SetSuspendState(bool hiberate, bool forceCritical, bool disableWakeEvent);

To bring your computer into Hibernate:

SetSuspendState(true, true, true);

And to bring it into sleep:

SetSuspendState(false, true, true);


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author


Belgium Belgium
No Biography provided

Comments and Discussions

GeneralMy vote of 5 PinmemberRobert J. Good2-Dec-14 13:05 
GeneralRe: My vote of 5 PinmvpProgramFOX3-Dec-14 5:30 
SuggestionYou can add hibernate and sleep options also PinmemberVirender Prajapati30-Nov-14 21:07 
GeneralRe: You can add hibernate and sleep options also PinmvpProgramFOX1-Dec-14 6:43 
Question+5 vote PinprofessionalMeher Khan10-Aug-14 0:20 
AnswerRe: +5 vote PinmvpProgramFOX17-Aug-14 0:32 
GeneralMy vote of 5 PinmemberGun Gun Febrianza23-Oct-12 22:27 
QuestionHello PinmemberGun Gun Febrianza23-Oct-12 22:26 
AnswerRe: Hello [modified] PinmemberProgramFOX24-Oct-12 3:05 
GeneralRe: Hello PinmemberGun Gun Febrianza24-Oct-12 6:18 
GeneralRe: Hello PinmemberProgramFOX24-Oct-12 6:21 
GeneralRe: Hello Pinmember Gun Gun Febrianza24-Oct-12 6:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150414.5 | Last Updated 1 Dec 2014
Article Copyright 2012 by ProgramFOX
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid