Warm Restart of the Windows OS

With this code you can restart both a local and a remote PC. As always, we cannot do without the class ManagementClass, which handles communication with the subsystem WMI, from which we obtain the object ManagementObjectCollection, whose first element is the WMI “Wrapper“. Then all you need to do is call, via invoke on the obtained object, the method Reboot or Shutdown, which are passed as strings. If you want to learn this interface in more detail, I recommend the MSDN documentation. Classes such as ManagementScope are located in System.Management.dll.

C#
using sunamo.System;
using System;
using System.Management;
using System.Windows.Forms;
namespace sunamo.System
{
    public class SystemUtils
    {
        public static void ShutdownSystem(string hostName, string remoteAuthUser,
            string remoteAuthPassword, bool reboot)
        {
            // If no host name is specified, use localhost
            if (hostName == null || hostName == "")
            {
                hostName = "localhost";
            }
            // Create a ManagementScope object, set its EnablePrivileges
            // property and pass the authentication information
            ManagementScope scope = new ManagementScope(@"\\" + hostName +
                "\root\cimv2");
            scope.Options.EnablePrivileges = true;
            scope.Options.Username = remoteAuthUser;
            scope.Options.Password = remoteAuthPassword;
            // Get the Win32_OperatingSystem instance for the active operating system
            ManagementClass managementClass = new ManagementClass(scope, new ManagementPath(
                    "Win32_OperatingSystem"), new ObjectGetOptions());
            ManagementObjectCollection managementObjects = managementClass.GetInstances();
            ManagementObjectCollection.ManagementObjectEnumerator
            managementObjectEnumerator = managementObjects.GetEnumerator();
            managementObjectEnumerator.MoveNext();
            ManagementObject managementObject = (ManagementObject)managementObjectEnumerator.Current;
            // Shut down or restart the computer using the Win32Shutdown method
            if (reboot)
            {
                managementObject.InvokeMethod("Reboot", null);
            }
            else
            {
                managementObject.InvokeMethod("Shutdown", null);
            }
            // Release the WMI objects to free up memory as soon as possible
            managementObject.Dispose();
            managementObjects.Dispose();
            managementClass.Dispose();
        }
    }
}
namespace Restart_Windows
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            Console.Title = "Shutting down and restarting Windows";
            // Host name and authentication information
            // The authentication information is used only for
            // remote access. Local access always
            // runs under the account of the current process.
            string hostName = "localhost";
            string authUser = null;
            string authPassword = null;
            /* Shut down and restart the computer */
            try
            {
                switch (MessageBox.Show(hostName + ": Do you want to shut down the operating system (Yes) or restart it (No)?",
                        Application.ProductName, MessageBoxButtons.YesNoCancel,
                        MessageBoxIcon.Question))
                {
                    case DialogResult.Yes:
                    SystemUtils.ShutdownSystem(hostName, authUser, authPassword, false);
                    break;
                    case DialogResult.No:
                    SystemUtils.ShutdownSystem(hostName, authUser, authPassword, true);
                    break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

 

Categories

Leave a Reply