How could I restart or shutdown Windows using the .NET framework?
The following code will execute the shutdown command from the shell:
// using System.Diagnostics; class Shutdown { /// <summary> /// Windows restart /// </summary> public static void Restart() { StartShutDown("-f -r -t 5"); } /// <summary> /// Log off. /// </summary> public static void LogOff() { StartShutDown("-l"); } /// <summary> /// Shutting Down Windows /// </summary> public static void Shut() { StartShutDown("-f -s -t 5"); } private static void StartShutDown(string param) { ProcessStartInfo proc = new ProcessStartInfo(); proc.FileName = "cmd"; proc.WindowStyle = ProcessWindowStyle.Hidden; proc.Arguments = "/C shutdown " + param; Process.Start(proc); } }
(Source: http://dotnet-snippets.de/dns/c-windows-herrunterfahren-ausloggen-neustarten-SID455.aspx
I don't know a pure .NET way to do it. Your options include:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With