shutdown my system by program
How do i force to shutdown my system by program
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Selva GanapathyPosted Nov 21, 2014, 8:05 AM
Hi Jegan Priya,
Please try the following code
[DllImport("user32")]
public static extern void LockWorkStation();
Process.Start("shutdown", "/s /t 0"); //System to shutdown
Process.Start("shutdown", "/r /t 0"); //System to restart
LockWorkStation(); //System to lock
Regards,
Selva Ganapathy K
Chandrashekar ShirasangiPosted Nov 21, 2014, 8:02 PM
Joginder BangerPosted Nov 21, 2014, 3:29 AM
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); } }
Posted Nov 21, 2014, 2:53 AM
Process.Start("shutdown","/s /t 0");
The process class comes under System.Diagnostics namespace
Kindly mark as answer, if it solves your problem