karthikSah
karthikSah

Reputation:

How do I shutdown the computer?

How to shutdown my computer using C#?

Upvotes: 6

Views: 3600

Answers (7)

Stefan Steiger
Stefan Steiger

Reputation: 82336

You could 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);

    }
}

Upvotes: 0

Aleris
Aleris

Reputation: 8069

WindowsController is a c# wrapper class around ExitWindowsEx.

Sometimes you need to restart or shutdown the operating system from your applications (for instance, after the installation of a program). The .NET framework offers you an indirect way to restart the computer through the Windows Management Instrumentation (WMI) classes in the System.Management namespace, however, there seem to be some problems in their implementation.

That's why we created the WindowsController class that implements some API functions to restart and shutdown Windows. It supports all the ExitWindowsEx modes and it can also hibernate and suspend the system.

This class is available in C# and VB.NET version. It can be compiled to a .NET module or to a library to be used from other .NET languages. Since it relies on the Windows API, it will not work on Linux or FreeBSD.

(mentalis.org)

Upvotes: 4

lakshmanaraj
lakshmanaraj

Reputation: 4175

Different methods:

A. 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 have seen so many others also have posted...

Upvotes: 5

Marko
Marko

Reputation: 31473

The hard way, works on laptops perfectly, 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.

:)

Upvotes: 0

Alnitak
Alnitak

Reputation: 339985

Use a variation of the "user logoff" code shown here.

That code uses the ExitWindowsEx API call.

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1064044

At a guess (untested):

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

Upvotes: 0

driis
driis

Reputation: 164341

An easy way: Use Process.Start to run shutdown.exe.

  shutdown /s /t 0

Programmatic way: P/Invoke a call to ExitWindowsEx

This would be the P/Invoke signature:

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

Under all circumstances, the user running the code will need shutdown system privileges (normally not a problem, but an important point to remember).

Upvotes: 10

Related Questions