Click here to Skip to main content
15,565,403 members
Articles / Programming Languages / C#
Tip/Trick
Posted 19 Oct 2012

Stats

212K views
9.2K downloads
53 bookmarked

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

Rate me:
Please Sign up or sign in to vote.
4.94/5 (25 votes)
1 Dec 2014CPOL
A tip about how to shut down, restart, log off, lock, hibernate or sleep your computer in C#.

Introduction

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:

C#
using System.Diagnostics;
using System.Runtime.InteropServices; 

To shut down your computer, use this code:

C#
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:

C#
Process.Start("shutdown", 
"/r /t 0"); // the argument /r is to restart the computer

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

C#
[DllImport("user32")]
public static extern bool ExitWindowsEx(uint uFlags, uint dwReason);

Then, to log off, invoke the method:

C#
ExitWindowsEx(0,0);

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

C#
[DllImport("user32")]
public static extern void LockWorkStation();

Then, to lock, invoke the method:

C#
LockWorkStation(); 

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!

C#
[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:

C#
SetSuspendState(true, true, true);

And to bring it into sleep:

C#
SetSuspendState(false, true, true);

License

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


Written By
Student
Europe Europe
Also known as ProgramFOX. I like programming, playing chess and astronomy. Administrator of Chess Variants Training[^].

Find me on:

Comments and Discussions

 
QuestionThanks so much :) Pin
Ertuğrul Baran13-May-22 5:01
Ertuğrul Baran13-May-22 5:01 
QuestionInteresting, what about reverse ? Pin
Gluups12-Jul-19 7:24
Gluups12-Jul-19 7:24 
AnswerRe: Interesting, what about reverse ? Pin
Thomas Daniels12-Jul-19 7:43
mentorThomas Daniels12-Jul-19 7:43 
GeneralRe: Interesting, what about reverse ? Pin
Gluups16-Jul-19 0:09
Gluups16-Jul-19 0:09 
Questionsafe mode? Pin
Радослав Ненчовски30-Nov-17 19:29
Радослав Ненчовски30-Nov-17 19:29 
AnswerRe: safe mode? Pin
Thomas Daniels2-Dec-17 4:34
mentorThomas Daniels2-Dec-17 4:34 
QuestionOther options Pin
Yves Goergen1-Mar-16 11:27
Yves Goergen1-Mar-16 11:27 
QuestionThanks. Pin
tinychi15-Jan-16 20:33
tinychi15-Jan-16 20:33 
AnswerRe: Thanks. Pin
Thomas Daniels15-Jan-16 20:37
mentorThomas Daniels15-Jan-16 20:37 
QuestionHibernate Condition (My vote of 5 for simplicity) Pin
Vekool_Coder23-Sep-15 20:02
Vekool_Coder23-Sep-15 20:02 
GeneralMy vote of 5 Pin
Robert J. Good2-Dec-14 13:05
professionalRobert J. Good2-Dec-14 13:05 
GeneralRe: My vote of 5 Pin
Thomas Daniels3-Dec-14 5:30
mentorThomas Daniels3-Dec-14 5:30 
SuggestionYou can add hibernate and sleep options also Pin
Virender Prajapati30-Nov-14 21:07
professionalVirender Prajapati30-Nov-14 21:07 
GeneralRe: You can add hibernate and sleep options also Pin
Thomas Daniels1-Dec-14 6:43
mentorThomas Daniels1-Dec-14 6:43 
Question+5 vote Pin
Meher Khan10-Aug-14 0:20
professionalMeher Khan10-Aug-14 0:20 
AnswerRe: +5 vote Pin
Thomas Daniels17-Aug-14 0:32
mentorThomas Daniels17-Aug-14 0:32 
GeneralMy vote of 5 Pin
Gun Gun Febrianza23-Oct-12 22:27
Gun Gun Febrianza23-Oct-12 22:27 
QuestionHello Pin
Gun Gun Febrianza23-Oct-12 22:26
Gun Gun Febrianza23-Oct-12 22:26 
AnswerRe: Hello Pin
Thomas Daniels24-Oct-12 3:05
mentorThomas Daniels24-Oct-12 3:05 
GeneralRe: Hello Pin
Gun Gun Febrianza24-Oct-12 6:18
Gun Gun Febrianza24-Oct-12 6:18 
GeneralRe: Hello Pin
Thomas Daniels24-Oct-12 6:21
mentorThomas Daniels24-Oct-12 6:21 
GeneralRe: Hello Pin
Gun Gun Febrianza24-Oct-12 6:25
Gun Gun Febrianza24-Oct-12 6:25 

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

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