A 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.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This 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.
Creating 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.
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) |
||
|
|||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|||
|
|||
Current CommentsThis will also reboot the system if on line 29 Flags is set to "2".
not working for c sharp 2002?
not working for c sharp 2002?
really its a good coding thanku
Code actually works, but one have to add a reference to System.Management.
Very useful, thanks.
Application.Run(new Form1()); //in this part show me error
Hi can anyone say how to run above code, i dont have idea about this
Code does not work on framework 1.1
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);
}
}
}
}
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);
}
}
}
}
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);
}
}
}
}
Please add Reference in ur Application.It is working for me......System.Management include and You have to add namespace in the Application Given.
Thanks for such a nice code.
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
System.Diagnostics.Process.Start("shutdown.exe","-s -t 3");
Shut down the system within 3 seconds
System.Diagnostics.Process.Start("shutdown.exe","-s -t 3");
Shut down the system within 3 seconds
Hi everyone!
How i add a library? i haven“t System.Management, System.Drawing, Windows.Form
Thanks in advance!
http://forums.devx.com/archive/index.php/t-56890.html
i wish to send me all things
can any one tell me the flag values or other methods for logoff, hibernate, Lock and standby Operations
tanx very very very much my dear friend
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
Nice code..
I got Exception "Privilage not held " Please Help
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
Easy with System Shutdown from Nostalgia .Net:
http://greatis.com/dotnet/nostalgia/windowsshutdown.html
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.
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..
Easiest way to shutdown computer is just this
->Process.Start("shutdown.exe");
Easiest way to shutdown computer is just this
->Process.Start("shutdown.exe");
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.
Related Source Code
Related Tutorials
C# Job SearchFrom 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.