Click here to Skip to main content
14,029,821 members
Click here to Skip to main content
Add your own
alternative version

Stats

164.5K views
8.3K downloads
50 bookmarked
Posted 19 Oct 2012
Licenced CPOL

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

, 1 Dec 2014
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#.

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:

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:

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

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

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

Then, to log off, invoke the method:

ExitWindowsEx(0,0);

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

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

Then, to lock, invoke the method:

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!

[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);

License

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

Share

About the Author

Thomas Daniels
Student
Belgium Belgium
Also known as ProgramFOX. I like programming, playing chess and astronomy. Administrator of Chess Variants Training[^].

Find me on:

You may also be interested in...

Comments and Discussions

 
Questionsafe mode? Pin
Радослав Ненчовски30-Nov-17 18:29
memberРадослав Ненчовски30-Nov-17 18:29 
AnswerRe: safe mode? Pin
Thomas Daniels2-Dec-17 3:34
mveThomas Daniels2-Dec-17 3:34 
QuestionOther options Pin
Yves Goergen1-Mar-16 10:27
memberYves Goergen1-Mar-16 10:27 
QuestionThanks. Pin
tinychi15-Jan-16 19:33
membertinychi15-Jan-16 19:33 
AnswerRe: Thanks. Pin
Thomas Daniels15-Jan-16 19:37
mveThomas Daniels15-Jan-16 19:37 
QuestionHibernate Condition (My vote of 5 for simplicity) Pin
Vekool_Coder23-Sep-15 19:02
memberVekool_Coder23-Sep-15 19:02 
GeneralMy vote of 5 Pin
Robert J. Good2-Dec-14 12:05
professionalRobert J. Good2-Dec-14 12:05 
GeneralRe: My vote of 5 Pin
Thomas Daniels3-Dec-14 4:30
mveThomas Daniels3-Dec-14 4:30 
SuggestionYou can add hibernate and sleep options also Pin
Virender Prajapati30-Nov-14 20:07
professionalVirender Prajapati30-Nov-14 20:07 
GeneralRe: You can add hibernate and sleep options also Pin
Thomas Daniels1-Dec-14 5:43
mveThomas Daniels1-Dec-14 5:43 
Question+5 vote Pin
Meher Khan9-Aug-14 23:20
professionalMeher Khan9-Aug-14 23:20 
AnswerRe: +5 vote Pin
Thomas Daniels16-Aug-14 23:32
mveThomas Daniels16-Aug-14 23:32 
GeneralMy vote of 5 Pin
Gun Gun Febrianza23-Oct-12 21:27
member Gun Gun Febrianza23-Oct-12 21:27 
QuestionHello Pin
Gun Gun Febrianza23-Oct-12 21:26
member Gun Gun Febrianza23-Oct-12 21:26 
AnswerRe: Hello Pin
Thomas Daniels24-Oct-12 2:05
mveThomas Daniels24-Oct-12 2:05 
GeneralRe: Hello Pin
Gun Gun Febrianza24-Oct-12 5:18
member Gun Gun Febrianza24-Oct-12 5:18 
GeneralRe: Hello Pin
Thomas Daniels24-Oct-12 5:21
mveThomas Daniels24-Oct-12 5:21 
GeneralRe: Hello Pin
Gun Gun Febrianza24-Oct-12 5:25
member Gun Gun Febrianza24-Oct-12 5: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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190419.4 | Last Updated 1 Dec 2014
Article Copyright 2012 by Thomas Daniels
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid