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

 

Navigate to code behind of form and add reference of System.Runtime.InteropServices

Add a static extern method to Form.cs

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.

 

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.

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.


Restart the System

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

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

For your reference full source code is given here

Form.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.

Advertisements

4 thoughts on “Shut Down, Restart, Log Off and Forced Log off System using c#

  1. Error 1 ‘SystmShutDownApp.Form1.Dispose(bool)’: no suitable method found to override C:\Documents and Settings\Samiran Saha\My Documents\Visual Studio 2005\Projects\WindowsApplication16\WindowsApplication16\Form1.Designer.cs 14 33 WindowsApplication16

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s