Simplest way to Programmatically ShutDown or Restart your System using C# or VB.NET

One of the ways to ShutDown or Restart your system is to use WMI (Windows Management Interface). However it does require you to write a few lines of code. I have found that the simplest way to ShutDown or Restart your computer is to use the Process class in the System.Diagnostics namespace. Here’s an example:

C#

private void btnShutDown_Click(object sender, EventArgs e)
{
Process.Start("shutdown", "-s -f -t 0");
}

private void btnRestart_Click(object sender, EventArgs e)
{
Process.Start("shutdown", "-r -f -t 0");
}

VB.NET

Private Sub btnShutDown_Click(ByVal sender As Object, ByVal e As EventArgs)
Process.Start("shutdown", "-s -f -t 0")
End Sub

Private Sub
btnRestart_Click(ByVal sender As Object, ByVal e As EventArgs)
Process.Start("shutdown", "-r -f -t 0")
End Sub



Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

No comments: