tags:

views:

549

answers:

5

Duplicate Answer

Shutdown a remote computer connected in LAN in any preferable language


I am wondering, Is it possible to Shut down a remote PC programatically through .net application?

If yes, how it can be?

+1  A: 

You can use WMI from your program for that.

Otávio Décio
+6  A: 

See this KB article

For example:

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents=false;
proc.StartInfo.FileName = "shutdown.exe";
proc.StartInfo.Arguments = @"\\computername /t:120 ""The computer is shutting down"" /y /c";
proc.Start();

The example above uses the following switches (excerpted from the linked article)

  • \\computername: Use this switch to specify the remote computer to shut down. If you omit this parameter, the local computer name is used.

  • /t:xx: Use this switch to specify the time (in seconds) after which the computer is shut down. The default is 20 seconds.

  • "The computer is shutting down": Use this switch to specify a message during the shutdown process. The maximum number of characters that the message can contain is 127.

  • /y: Use this switch to force a "yes" answer to all queries from the computer.

  • /c: Use this switch quit all running programs. If you use this switch, Windows forces all programs that are running to quit. The option to save any data that may have changed is ignored. This can result in data loss in any programs for which data is not previously saved.

Patrick McDonald
Thanks for the fix Samuel :)
Patrick McDonald
I thought your excerpt from the article was fine. Especially when explaining the switches you were using in your code snippet. Microsoft may restructure their website and break the link. Oh well... At least it was you editing it down and not some over zealous modder with edit rights :)
wcm
@wcm, thanks for your comment, I put the relevant bits of the excerpt back in and fixed the switches
Patrick McDonald
+2  A: 

WMI will work for this task. The Win32_OperatingSystem class has a method called Win32Shutdown. This can be used to shutdown a remote computer.

Win32Shutdown: http://msdn.microsoft.com/en-us/library/aa394058(VS.85).aspx

The following blog entry has a full program in C# that demonstrates how to shutdown a computer using WMI. Search for Win32Shutdown on the page (about the 2nd article down)

http://urkec.mojblog.rs/

JaredPar
A: 

My way:

Process.Start("ShutDown", "/s");

Fired from a remote ASP.NET page, of course.

Dmitri Nesteruk
I think this will shut down the server rather than the remote PC
Conrad
this shutdowns the current pc, not the remote pc
PoweRoy
A: 

I have a local application running with an user than have a group policy indicating this user can’t shutdown the pc. And then I use your code, but when I ask by user and password to impersonate my app. I received the next error System.Management.ManagementException: it’s not possible to use the user’s credentials for the local connections

neo