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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Selva GanapathyRank #560 · 4.3K reputationPosted Nov 21, 2014, 3:05 AM EST
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
Posted Nov 20, 2014, 9:53 PM EST
Process.Start("shutdown","/s /t 0");
The process class comes under System.Diagnostics namespace
Kindly mark as answer, if it solves your problem
Joginder BangerRank #360 · 7K reputationPosted Nov 20, 2014, 10:29 PM EST
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); } }
Chandrashekar Shirasangi3 reputationPosted Nov 21, 2014, 3:02 PM EST