5

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.

user2262382
  • 335
  • 2
  • 5
  • 13

7 Answers7

3

// Remote Shutdown

    public bool RemoteShutdown(string userName, string password, string ip)
    {
        try
        {
            ConnectionOptions op = new ConnectionOptions();
            op.Username = userName;
            op.Password = password;
            // Make a connection to a remote computer.  
            ManagementScope scope = new ManagementScope("\\\\" + ip + "\\root\\cimv2", op);
            scope.Connect();
            //Query system for Operating System information  
            ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
            ManagementObjectSearcher query = new ManagementObjectSearcher(scope, oq);
            ManagementObjectCollection queryCollection = query.Get();
            foreach (ManagementObject obj in queryCollection)
            {
                obj.InvokeMethod("ShutDown", null); //shutdown  
            }

            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }

// Remote Reboot

    public bool RemoteReboot(string userName, string password, string ip)
    {
        try
        {
            ConnectionOptions op = new ConnectionOptions();
            op.Username = userName;
            op.Password = password;
            // Make a connection to a remote computer.  
            ManagementScope scope = new ManagementScope("\\\\" + ip + "\\root\\cimv2", op);
            scope.Connect();
            //Query system for Operating System information  
            ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
            ManagementObjectSearcher query = new ManagementObjectSearcher(scope, oq);
            ManagementObjectCollection queryCollection = query.Get();
            foreach (ManagementObject obj in queryCollection)
            {
                obj.InvokeMethod("Reboot", null); //shutdown  
            }

            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }
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'");

Sorayuki
  • 256
  • 2
  • 7
  • Hi still it doesn't work I tried to alter the codes many times but still it doesn't work I know that the network connection is ok since I tried running command prompt and it shutdown the remote computer. The only choice left for me is to execute the program using C# via .bat files which has remote shutdown commands on it. – user2262382 Apr 21 '13 at 09:12
1

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
Community
  • 1
  • 1
Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
  • may I know what is "Flags" all about and the numbers 2 and 1 I am clueless with that codes. – user2262382 Apr 21 '13 at 09:13
  • anyway, i forgot to open your link and just now I found out that this has a good content relevant to what I am doing right now. Thank you. – user2262382 Apr 21 '13 at 09:19
1

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

0

This is the code I use for shutdown / reboot remote machines:

int waitSeconds = 0;
string remoteHostNameOrIp = "192.168.0.1";
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.FileName = "shutdown.exe";
processInfo.Arguments = $@"-s -t { waitSeconds } -m \\{ remoteHostNameOrIp }";
processInfo.WindowStyle = ProcessWindowStyle.Hidden;
processInfo.CreateNoWindow = true;

var proc = Process.Start(processInfo);

// Works without WaitForExit(); 
// When using this you can check in "proc" if the shutdown was accepted by 
// the remote machine 
// 
// proc.WaitForExit();
marsh-wiggle
  • 2,508
  • 3
  • 35
  • 52
-1

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.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
-1

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

  • 1
    As others have already said, this is a Q&A site, not a forum. Every time you post an answer you should answer the question, and not start a sort of conversation about previous answers. That said, here you seem to have a solution, which would be fine, if only it was readable. Please visit the [help center](http://stackoverflow.com/editing-help#comment-formatting) and learn how to format your answer, then come back here and [edit] it. Also, if your answer is based on another one, add a link to it. The order is not always the same (for example it depends on votes). Thank you! – Fabio says Reinstate Monica Oct 28 '16 at 23:08