How to Lock, Logoff, Restart, Shutdown Computer using C# with Winform Example

Category > CSHARP || Published on : Sunday, May 10, 2015 || Views: 6658 || How to Lock Logoff Restart Shutdown Computer using C# with Winform Example


In this post I will show you how you can Lock, LogOff, Restart or Shutdown the computer using C#. It is very simple to do so because you just have to call the appropriate process and execute it inside your C# code.

So lets start the fun part that is the coding

Step 1: Create a Winform Project

Step 2: By default in solution explorer we have a form with name "Form1". Place four button from toolbox to "Form1". Same as the screenshot below

Step 3:  Add the namespace at the top of the page.
 

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.Diagnostics;
using System.IO;

Step 4: Double Click on on every button and write the code like below:-

* for Lock the computer in C#:-

 private void button1_Click(object sender, EventArgs e)
        {
            using (Process proc = new Process())
            {
                proc.StartInfo.FileName = Path.Combine(Environment.SystemDirectory, "rundll32.exe");
                proc.StartInfo.Arguments = "user32.dll,LockWorkStation";
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.CreateNoWindow = true;
                proc.Start();
            }
        }

* Logoff the computer in C#

  private void button2_Click(object sender, EventArgs e)
        {
            using (Process proc = new Process())
            {
                proc.StartInfo.FileName = Path.Combine(Environment.SystemDirectory, "shutdown.exe");
                proc.StartInfo.Arguments = "-l";
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.CreateNoWindow = true;
                proc.Start();
            }
        }

*  Restart the computer in C#

private void button3_Click(object sender, EventArgs e)
        {
            using (Process proc = new Process())
            {
                proc.StartInfo.FileName = Path.Combine(Environment.SystemDirectory, "shutdown.exe");
                proc.StartInfo.Arguments = "-r -t 0";
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.CreateNoWindow = true;
                proc.Start();
            }
        }

* Shutdown the computer in C#

 private void button4_Click(object sender, EventArgs e)
        {
            using (Process proc = new Process())
            {
                proc.StartInfo.FileName = Path.Combine(Environment.SystemDirectory, "shutdown.exe");
                proc.StartInfo.Arguments = "-s -t 0";
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.CreateNoWindow = true;
                proc.Start();
            }
        }

Conclusion:-
so you have learned to How to Lock, Logoff, Restart, Shutdown Computer using C# with Winform Example.... Happy Coding !!!!

 

Download Source Codes