Geekpedia Programming Tutorials






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.1 with 15 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


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 >>
Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons