How to turn off the computer?

How to disconnect my computer using C #?

+6
c # windows shutdown
source share
7 answers

Easy way: use Process.Start to start shutdown.exe.

shutdown /s /t 0 

Program Path: P / Call ExitWindowsEx Call

This will be the signature of P / Invoke:

 [DllImport("aygshell.dll", SetLastError="true")] private static extern bool ExitWindowsEx(uint dwFlags, uint dwReserved); 

Under any circumstances, the user executing the code will need the right to shut down the system (usually this is not a problem, but an important point to remember).

+10
source share

Various methods:

but. System.Diagnostics.Process.Start ("Shutdown", "-s -t 10");

B. Windows Management Instrumentation (WMI)

http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=36953

http://www.dreamincode.net/forums/showtopic33948.htm

C. System.Runtime.InteropServices Pinvoke

http://bytes.com/groups/net-c/251367-shutdown-my-computer-using-c

D. System Management

http://www.geekpedia.com/code36_Shut-down-system-using-Csharp.html

After I submit, I saw how many others also published ...

+5
source share

WindowsController is a C # wrapper class around ExitWindowsEx.

Sometimes you need to restart or shut down the operating system from your applications (for example, after installing the program) .. NET Framework offers you an indirect way to restart your computer through the Windows Management Instrumentation (WMI) in the System.Management namespace, however, there seem to be some problems in their implementation.

This is why we created the WindowsController Class, which implements some API functions for restarting and shutting down Windows. It supports all ExitWindowsEx modes and it can also sleep and suspend systems.

This class is available in C # and VB.NET Version. It can be compiled with a .NET module or a library that should be used in other .NET languages. since it relies on the Windows API, it will not work on Linux or FreeBSD.

(mentalis.org)

+3
source share

Use the user code option that is displayed here .

This code uses the ExitWindowsEx API ExitWindowsEx .

+1
source share

Assumption (untested):

 Process.Start("shutdown", "-s -t 0"); 
0
source share

The hard way, works great on laptops, although it takes some time:

 Spawn a couple endless loops in more threads than cpu cores. Wait for overheat which will automatically shutdown a computer. 

:)

0
source share

You can also use InitiateSystemShutdown

http://www.pinvoke.net/default.aspx/advapi32.initiatesystemshutdown

 using System; using System.Runtime.InteropServices; using System.Text; public class Program { [DllImport( "advapi32.dll" ) ] public static extern bool InitiateSystemShutdown( string MachineName , string Message , uint Timeout , bool AppsClosed , bool Restart ); [DllImport( "kernel32.dll" ) ] public static extern uint GetLastError(); [DllImport( "kernel32.dll" ) ] public static extern uint FormatMessage( uint Flags , IntPtr Source , uint MessageID , uint LanguageID , StringBuilder Buffer , uint Size , IntPtr Args ); public static void Main() { InitiateSystemShutdown(System.Environment.MachineName, "hello", 0, false, false); //InitiateSystemShutdown("localhost", "hello", 0, false, false); } } 
0
source share

All Articles