Remote shutdown PC using C# windows form?

2019-02-26 08:49发布

问题:

I'm trying to shutdown PC remotely programmatically using c# via command prompt and I already done a few search and found out this kind of codes.

System.Diagnostics.Process.Start("shutdown", "/s");

And since it doesn't spicify any pc which to shutdown so I tried changing that codes to this codes which I think satisfy my goal. But it turns out that this doesn't work.

System.Diagnostics.Process.Start("shutdown", "/s /m \\192.168.1.21 /t 5 /c 'Shutdown in 5 seconds'");

NO Exception in C# it just don't shutdown.

I also try this but no luck.

 System.Diagnostics.Process.Start("shutdown /s /m \\192.168.1.21 /t 5 /c 'Shutdown in 5 seconds'");

C# Exception "The system cannot find file specified".

EDIT:

I forgot to tell you that I alredy set up my remote computer and server the way that it will not fail to connect to each other such as turning off the firewall, configuring Local system policy and changing network and sharing center.

回答1:

in C#, \\ in string means \

so the parameter interpreted as

/s /m \192.168.1.21 /t 5 /c 'Shutdown in 5 seconds'

you should use \\\\ to represent \\

or add an @ mark before the start quote mark like this

System.Diagnostics.Process.Start("shutdown", @"/s /m \\192.168.1.21 /t 5 /c 'Shutdown in 5 seconds'");



回答2:

You can have a look at the SO post below. It talks about rebooting a remote machine.

WMI to reboot remote machine

If you look at the Win32Shutdown method

Shutdown => 1 (0x1) & Reboot => 2 (0x2)

So in the SO link above you will have to change

 // Add the input parameters.
 inParams["Flags"] =  2; //Reboot

to

 // Add the input parameters.
 inParams["Flags"] =  1; //Shutdown


回答3:

You must also enable the "Remote Registry" service on the target computer, AND you must have admin rights on the remote computer.

See here for details.



回答4:

This should work:

System.Diagnostics.Process.Start("shutdown", @"-m \\192.168.1.21 -s -f -t 0");

Flags should be syntaxed like -m , not /m.

Even better is to create a "silent" shutdown (without cmd-window to show):

var shutdown = new ProcessStartInfo("shutdown", @"-m \\192.168.1.8 -s -f -t 0");
shutdown.CreateNoWindow = true;
shutdown.UseShellExecute = false;
Process.Start(shutdown);

Tested in win7, .Net framework 4.03



回答5:

thanks for the createnowindow, i was already thinking to use it if everything works (but its unusefull if you want to see what is happening)

No comments without explaination about my dashes. as long as i know, you can choose between - or /

try it yourself :-) Start+R (run) you will see it works :)

i think its to much work to solve it.

So i figgerd out that a able to use a file "FILENAME.cmd"

with "shutdown -s -m \IP -t 40" in it

The delay for reminder to close or save stuff (not nessecary)

the -f doesn't work for me

but with the cmd file i can call it and let it run from my application.

So my problem is solved, i know its a little bit.... not perfect. but it works at least.

Thanx to all for quick reply's



标签: c# cmd shutdown