Remote shutdown PC using C# windows form?

杀马特。学长 韩版系。学妹 提交于 2019-12-01 22:54:39

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'");

Patrick D'Souza

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

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.

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

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!