Shut Down, Restart, Log Off and Forced Log off System using c#

No.of Views1055
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  03 Jun 2010 08:06:39
Tag : CSharp , Utilities
Shut Down, Restart, Log Off and Forced Log off System using c#
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Objective

In this article, I am going to show,

1.    How to Shut Down a machine
2.    How to Log Off a machine
3.    How to forced log off a machine
4.    How to restart a machine using c#

To perform our task, very first let us create a windows application project.   On form drag and drop four buttons for four different operations.  After design form will look like below 

Image Loading

Navigate to code behind of form and add reference of System.Runtime.InteropServices,Add a static extern method to Form.cs

 

Image Loading

Log off  the System

On click event of Log Off button, call ExitWindowsEX method with proper flag.  For log off  operation flag value is 0.

 

Image Loading

Forced Log off the System

On click event of Forced Log Off button, call ExitWindowsEX method with proper flag.  For Forced  log off  operation flag value is 4.

 

Image Loading

Shut Down the System

On click event of Shut down button, call ExitWindowsEX method with proper flag.  For shut down   operation flag value is 1.

 

Image Loading

Restart the System

On click event of Restart button, call ExitWindowsEX method with proper flag.  For Restart  operation flag value is 2.

 

Image Loading

Now when you run the application all system operation should be performed.

For your reference full source code is given here

Form1.cs

 

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;namespace SystmShutDownApp
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){}[DllImport("user32.dll")]public static extern int ExitWindowsEx(int operationFlag, int operationReason);private void btnRestart_Click(object sender, EventArgs e){ExitWindowsEx(2, 0);}private void btnLogOff_Click(object sender, EventArgs e){ExitWindowsEx(0, 0);}private void btnForcedLogOff_Click(object sender, EventArgs e){ExitWindowsEx(4, 0);}private void btnShutDown_Click(object sender, EventArgs e){ExitWindowsEx(1, 0);}}}

Thanks for reading. I hope post was useful. Happy Coding. Here is my blog

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
By:chaussures nikeDate Of Posted:3/23/2011 2:16:35 AM
chaussures nike
Thank you for sharing this information, which I learned a lot, but also know a lot of knowledge. I hope I'll still see your information next time.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top