Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Shut down system using C#

This code shut downs the operating system using the System.Management assembly, but not before obtaining the required security privileges.

On Saturday, October 13th 2007 at 12:37 AM
By Andrew Pociu (View Profile)
*****   (Rated 4.3 with 20 votes)
Contextual Ads
More C# Resources
Advertisement
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. // Remember to add a reference to the System.Management assembly
  9. using System.Management;
  10.  
  11. namespace ShutDown
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         private void btnShutDown_Click(object sender, EventArgs e)
  21.         {
  22.             ManagementBaseObject mboShutdown = null;
  23.             ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
  24.             mcWin32.Get();
  25.             // You can't shutdown without security privileges
  26.             mcWin32.Scope.Options.EnablePrivileges = true;
  27.             ManagementBaseObject mboShutdownParams = mcWin32.GetMethodParameters("Win32Shutdown");
  28.             // Flag 1 means we want to shut down the system
  29.             mboShutdownParams["Flags"] = "1";
  30.             mboShutdownParams["Reserved"] = "0";
  31.             foreach (ManagementObject manObj in mcWin32.GetInstances())
  32.             {
  33.                 mboShutdown = manObj.InvokeMethod("Win32Shutdown", mboShutdownParams, null);
  34.             }
  35.         }
  36.     }
  37. }
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this code snippet
Comment Current Comments
by Ron Simon on Tuesday, January 15th 2008 at 09:22 AM

This will also reboot the system if on line 29 Flags is set to "2".

by fabiano on Thursday, April 24th 2008 at 10:24 AM

not working for c sharp 2002?

by fabiano on Thursday, April 24th 2008 at 10:24 AM

not working for c sharp 2002?

by dharan on Tuesday, May 27th 2008 at 07:54 AM

really its a good coding thanku

by Pablo on Monday, June 9th 2008 at 07:40 AM

Code actually works, but one have to add a reference to System.Management.
Very useful, thanks.

by daniel on Wednesday, July 9th 2008 at 02:37 AM




Application.Run(new Form1()); //in this part show me error

by Arun on Tuesday, July 15th 2008 at 04:05 PM

Hi can anyone say how to run above code, i dont have idea about this

by george on Thursday, December 11th 2008 at 11:07 AM

Code does not work on framework 1.1

by trieutuduong on Thursday, December 11th 2008 at 02:24 PM

you can explain every command for me :namespace ShutDown
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnShutDown_Click(object sender, EventArgs e)
{
ManagementBaseObject mboShutdown = null;
ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
mcWin32.Get();
// You can't shutdown without security privileges
mcWin32.Scope.Options.EnablePrivileges = true;
ManagementBaseObject mboShutdownParams = mcWin32.GetMethodParameters("Win32Shutdown");
// Flag 1 means we want to shut down the system
mboShutdownParams["Flags"] = "1";
mboShutdownParams["Reserved"] = "0";
foreach (ManagementObject manObj in mcWin32.GetInstances())
{
mboShutdown = manObj.InvokeMethod("Win32Shutdown", mboShutdownParams, null);
}
}
}
}

by trieutuduong on Thursday, December 11th 2008 at 02:24 PM

you can explain every command for me :namespace ShutDown
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnShutDown_Click(object sender, EventArgs e)
{
ManagementBaseObject mboShutdown = null;
ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
mcWin32.Get();
// You can't shutdown without security privileges
mcWin32.Scope.Options.EnablePrivileges = true;
ManagementBaseObject mboShutdownParams = mcWin32.GetMethodParameters("Win32Shutdown");
// Flag 1 means we want to shut down the system
mboShutdownParams["Flags"] = "1";
mboShutdownParams["Reserved"] = "0";
foreach (ManagementObject manObj in mcWin32.GetInstances())
{
mboShutdown = manObj.InvokeMethod("Win32Shutdown", mboShutdownParams, null);
}
}
}
}

by corema on Tuesday, May 5th 2009 at 02:09 PM

sounds good but i don't get it to work..
ManagementBaseObject and ManagementClass couldn't be found
i allready included System.Management
here's my source;
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;
// Remember to add a reference to the System.Management assembly
using System.Management;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnShutDown_Click(object sender, EventArgs e)
{
ManagementBaseObject mboShutdown = null;
ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
mcWin32.Get();
// You can't shutdown without security privileges
mcWin32.Scope.Options.EnablePrivileges = true;
ManagementBaseObject mboShutdownParams = mcWin32.GetMethodParameters("Win32Shutdown");
// Flag 1 means we want to shut down the system
mboShutdownParams["Flags"] = "1";
mboShutdownParams["Reserved"] = "0";
foreach (ManagementObject manObj in mcWin32.GetInstances())
{
mboShutdown = manObj.InvokeMethod("Win32Shutdown", mboShutdownParams, null);
}
}
}
}

by Selvaraj Prabhu on Thursday, August 13th 2009 at 06:16 AM

Please add Reference in ur Application.It is working for me......System.Management include and You have to add namespace in the Application Given.

by Yogesh on Friday, August 21st 2009 at 09:45 AM

Thanks for such a nice code.

by amit pathak on Saturday, September 19th 2009 at 01:32 PM

sounds good but i don't get it to work..
ManagementBaseObject and ManagementClass couldn't be found
i allready included System.Management
here's my source;
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;
// Remember to add a reference to the System.Management assembly
using System.Management;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnShutDown_Click(object sender, EventArgs e)
{
ManagementBaseObject mboShutdown = null;
ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
mcWin32.Get();
// You can't shutdown without security privileges
mcWin32.Scope.Options.EnablePrivileges = true;
ManagementBaseObject mboShutdownParams = mcWin32.GetMethodParameters("Win32Shutdown");
// Flag 1 means we want to shut down the system
mboShutdownParams["Flags"] = "1";
mboShutdownParams["Reserved"] = "0";
foreach (ManagementObject manObj in mcWin32.GetInstances())
{
mboShutdown = manObj.InvokeMethod("Win32Shutdown", mboShutdownParams, null);
}
}
}
}

i think it will work after removing these error, if any one suggest some suggest, i m very pleased with that one... thank you

by ramya on Saturday, December 19th 2009 at 01:00 AM

System.Diagnostics.Process.Start("shutdown.exe","-s -t 3");


Shut down the system within 3 seconds

by ramya on Saturday, December 19th 2009 at 01:01 AM

System.Diagnostics.Process.Start("shutdown.exe","-s -t 3");


Shut down the system within 3 seconds

by Markos on Friday, January 8th 2010 at 08:00 AM

Hi everyone!
How i add a library? i havenĀ“t System.Management, System.Drawing, Windows.Form

Thanks in advance!

by Misza on Thursday, February 25th 2010 at 09:12 AM


http://forums.devx.com/archive/index.php/t-56890.html

by dina on Monday, July 5th 2010 at 05:09 AM

i wish to send me all things

by raghu on Thursday, August 12th 2010 at 02:50 AM

can any one tell me the flag values or other methods for logoff, hibernate, Lock and standby Operations

by ehsan on Tuesday, October 5th 2010 at 05:28 AM

tanx very very very much my dear friend

by Dali on Wednesday, October 20th 2010 at 10:54 AM

The Win32Shutdown WMI class method provides the full set of shutdown options supported by Win32 operating systems. These include logoff, shutdown, reboot, and forcing a logoff, shutdown, or reboot.
N.B:The calling process must have the SE_SHUTDOWN_NAME privilege.

Flags:
- 0 (0x0) Log Off
- 4 (0x4) Forced Log Off (0 4)
- 1 (0x1) Shutdown
- 5 (0x5) Forced Shutdown (1 4)
- 2 (0x2) Reboot
- 6 (0x6) Forced Reboot (2 4)
- 8 (0x8) Power Off
- 12 (0xC) Forced Power Off (8 4)

http://msdn.microsoft.com/en-us/library/aa394058(v=VS.85).aspx

by Arpan jain on Monday, November 1st 2010 at 01:37 PM

Nice code..

by Praful on Saturday, December 4th 2010 at 06:10 AM

I got Exception "Privilage not held " Please Help

by Praful on Saturday, December 4th 2010 at 06:11 AM

I got Exception "Privilage not held " Please Help
I Try to Solve it since 10 days but no success ,
please help friends . Thanx To ALL in Advance

by Greatis on Monday, October 24th 2011 at 12:02 PM

Easy with System Shutdown from Nostalgia .Net:

http://greatis.com/dotnet/nostalgia/windowsshutdown.html

by Squidoo Lens creation on Sunday, December 18th 2011 at 11:57 AM

Since this method only returns the age in years, it should return 0, and i think that's ok because the child is 1 day 0 years old? and thx for instructiv code

other question :
if a child is born this morning what is then most probable date of conception ? Nate,

Minor nitpick, but that could make the calculation incorrect for leap years.

by baji on Friday, July 20th 2012 at 12:42 AM

Thank you very much dude... This code is executing.. And don't forget to add references System.Management.
if possible try to explain about code..

by [DocNet] on Thursday, August 2nd 2012 at 10:36 AM

Easiest way to shutdown computer is just this

->Process.Start("shutdown.exe");

by [DocNet] on Thursday, August 2nd 2012 at 10:36 AM

Easiest way to shutdown computer is just this

->Process.Start("shutdown.exe");

by GrimesMilagros25 on Sunday, September 30th 2012 at 05:17 AM

I think that to receive the personal loans from banks you should have a great reason. Nevertheless, once I've got a short term loan, because I wanted to buy a bike.


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Source Code
There is no related code.

Comment Related Tutorials
There are no related tutorials.

Jobs C# Job Search
My skills include:

Enter a City:

Select a State:


Advanced Search >>
Ads

From the creators of Geekpedia, a revolutionary new coupon website!

BargainEZ has coupons codes, printable coupons, bargains and it is the leading source of Passbook coupons for iPhone and iPod touch devices.

Coupons
Discover Geekpedia
Other Resources