Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
How can I shutdown my computer using C#?
Posted
Updated 23-Oct-10 3:00am
v2
Comments
Sandeep Mewara 23-Oct-10 4:59am    
Google?

1 solution

Hi,

there are several possibilities, here are 2 I can think of:

1. using WMI:
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);
        }
    }


2. using Process:

System.Diagnostics.Process.Start("shutdown","/s /t 0");
 
Share this answer
 
Comments
LloydA111 23-Oct-10 9:19am    
I don't see why you was downvoted for this! So I upvoted you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900