You need to be careful with code like this - your service needs to check if its in an interactive session, otherwise your machine will boot then shutdown after X seconds, even if nobody is logged-on to the machine.
On Thu, Apr 22, 2010 at 2:59 PM, Benj Nunez <[email protected]> wrote: > Try to design it like a service and use this code: > > > ... > using System.Management; > ... > > void shutdown() > { > 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. Use "2" > to reboot. > mboShutdownParams["Flags"] = "1"; > mboShutdownParams["Reserved"] = "0"; > foreach (ManagementObject manObj in > mcWin32.GetInstances()) > { > mboShutdown = manObj.InvokeMethod("Win32Shutdown", > mboShutdownParams, > null); > } > } > > > > Cheers! > > > Benj > > > > > > On Apr 22, 1:44 pm, venkat ramana <[email protected]> wrote: >> Check the link below >> >> http://www.codeproject.com/KB/cs/uim.aspx >> >> -- >> Venkat. >> ph - 9810447656 >> >> On Wed, Apr 21, 2010 at 8:04 PM, Stephen Russell >> <[email protected]>wrote: >> >> > On Wed, Apr 21, 2010 at 9:10 AM, B☼gus Excepti☼n <[email protected]> >> > wrote: >> > > Interested in your thoughts on this: >> >> > > Given: >> >> > > -svr 2008 >> > > -Single user, almost always Administrator >> >> > > Need: >> >> > > -detect x minutes inactivity, then.. >> > > -shutdown the server >> >> > > I need this program to run as a service, as using the scheduler is too >> > > easy to defeat. >> > --------------------------------- >> >> > Think you are crazy. Why shut down instead of log off? >> >> > -- >> > Stephen Russell >> >> > Sr. Production Systems Programmer >> > CIMSgts >> >> > 901.246-0159 cell >> >> > -- >> > Subscription settings: >> >http://groups.google.com/group/dotnetdevelopment/subscribe?hl=en >
