how to shut down system programatically

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Anyone have any code examples (C# preferably) on how to shut down the system
or info on what namspace and class to use?
Thanks
gv
 
Nope, not yet anyway. The framework is still being completed and there's a
lot of functionality still missing. John Paul Mueller wrote a wonderful
book called .NET Framework Solutions: In Search of hte Lost Win32 API
http://www.amazon.com/exec/obidos/tg/detail/-/078214134X/qid=1079634711/sr=8
-1/ref=sr_8_xs_ap_i1_xgl14/002-0113426-9720057?v=glance&s=books&n=507846
That covers all of the P/INVOKE Stuff like this that is still missing (not
really all, but a heck of a lot)
 
Yes there is, take a look at the System.Management classes and the WMI
"win32_OperatingSystem" class 'shutdown" method.
Following example illustrates how to call Shutdown using System.Management
and WMI:

using System;
using System.Management;
using System.Runtime.InteropServices;
public class Shutdown {
public static void Main() {
string osName = null;
ManagementObjectSearcher searcher =
new ManagementObjectSearcher( "root/cimv2", "select * from
Win32_OperatingSystem where primary=true");
ManagementObjectCollection collection = searcher.Get();
foreach (ManagementObject item in collection)
{
osName = item.Properties["Name"].Value.ToString();
}
using (ManagementObject os = new
ManagementObject("Win32_OperatingSystem.Name='" + osName +"'"))
{
ManagementBaseObject inParams = null;
os.Scope.Options.EnablePrivileges = true;
ManagementBaseObject outParams = os.InvokeMethod("Shutdown", inParams,
null);
if ((System.UInt32)(outParams.Properties["ReturnValue"].Value) != 0)
{
// Failed check return code
}
}
}
}

Willy.
 
Much thanks, this works as hoped.


Willy Denoyette said:
Yes there is, take a look at the System.Management classes and the WMI
"win32_OperatingSystem" class 'shutdown" method.
Following example illustrates how to call Shutdown using System.Management
and WMI:

using System;
using System.Management;
using System.Runtime.InteropServices;
public class Shutdown {
public static void Main() {
string osName = null;
ManagementObjectSearcher searcher =
new ManagementObjectSearcher( "root/cimv2", "select * from
Win32_OperatingSystem where primary=true");
ManagementObjectCollection collection = searcher.Get();
foreach (ManagementObject item in collection)
{
osName = item.Properties["Name"].Value.ToString();
}
using (ManagementObject os = new
ManagementObject("Win32_OperatingSystem.Name='" + osName +"'"))
{
ManagementBaseObject inParams = null;
os.Scope.Options.EnablePrivileges = true;
ManagementBaseObject outParams = os.InvokeMethod("Shutdown", inParams,
null);
if ((System.UInt32)(outParams.Properties["ReturnValue"].Value) != 0)
{
// Failed check return code
}
}
}
}

Willy.

The .Net Framework doesn't have a class that does this on its own?
 
Back
Top