If i were tto implement somthing like this :idea:

cmd \k shutdown.exe -r

on a button click would that saftley restart y comp. from that app. if not how would i do this :?:

Dani AI

Generated

Quick summary and what to watch for: ’s idea (calling the shutdown utility from a button) does work, and ’s example shows the simplest route. The follow-ups from expose the usual pitfalls: missing action flags, string-escaping, and permissions. and are right that using the Win32 API via P/Invoke gives more control and avoids some UAC/permission surprises.

Common causes when a remote or delayed shutdown “does nothing”

  • shutdown.exe needs an action flag (restart or shutdown). Supplying only -m \\COMPUTERNAME won’t trigger anything.
  • In C# string literals the UNC form must be escaped. Either use a verbatim string: @"-m \\COMPUTERNAME -r -t 10" or escape backslashes: "-m \\\\COMPUTERNAME -r -t 10".
  • Remote shutdown requires administrative rights on the target and the correct RPC/firewall configuration — failures often show “Access is denied.”
  • -t only controls the timeout for a shutdown action; pair it with -r or -s. To diagnose, run the same command manually and/or capture stderr/stdout from the child process to read any error message.

When to use the API (recommended for production or remote/admin tasks)

  • Call the shutdown APIs directly (via DllImport) for finer control and fewer surprises from UAC. Example (advapi32):
[DllImport("advapi32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern bool InitiateSystemShutdownEx(
    string lpMachineName,
    string lpMessage,
    uint dwTimeout,
    bool bForceAppsClosed,
    bool bRebootAfterShutdown,
    uint dwReason
);

// Local reboot in 10s:
InitiateSystemShutdownEx(null, "Reboot requested by app", 10, true, true, 0);

// Remote reboot:
InitiateSystemShutdownEx(@"\\COMPUTERNAME", "Reboot requested by app", 10, true, true, 0);

Cautions and final tips

  • The API call requires elevation / the SE_SHUTDOWN_NAME privilege; on Vista+ run elevated or enable the privilege explicitly. Test carefully (forcing apps closed will lose unsaved data). For quick one-off tasks, continuing to invoke shutdown.exe (with properly escaped args and elevated run) is fine; for robust tools prefer the API.

Recommended Answers

All 11 Replies

google is letting me down :(

try this:

using System.Diagnostics;

ProcessStartInfo startinfo = new ProcessStartInfo("shutdown.exe","-r");
Process.Start(startinfo);

try this:

using System.Diagnostics;

ProcessStartInfo startinfo = new ProcessStartInfo("shutdown.exe","-r");
Process.Start(startinfo);

hmmm the r stands for restart so is l logoff?

-i                      Display GUI interface, must be the first option
        -l                      Log off (cannot be used with -m option)
        -s                      Shutdown the computer
        -r                      Shutdown and restart the computer
        -a                      Abort a system shutdown
        -m \\computername       Remote computer to shutdown/restart/abort
        -t xx                   Set timeout for shutdown to xx seconds
        -c "comment"            Shutdown comment (maximum of 127 characters)
        -f                      Forces running applications to close without war
ning
        -d [u][p]:xx:yy         The reason code for the shutdown
                                u is the user code
                                p is a planned shutdown code
                                xx is the major reason code (positive integer le
ss than 256)
                                yy is the minor reason code (positive integer le
ss than 65536)

-l would be logoff

Tahnk you and is thissafe for the computer?

yeah its safe, its no different then going to cmd and using it

yeah its safe, its no different then going to cmd and using it

lol ok thanks so i guess its safe there

-i                      Display GUI interface, must be the first option
        -l                      Log off (cannot be used with -m option)
        -s                      Shutdown the computer
        -r                      Shutdown and restart the computer
        -a                      Abort a system shutdown
        -m \\computername       Remote computer to shutdown/restart/abort
        -t xx                   Set timeout for shutdown to xx seconds
        -c "comment"            Shutdown comment (maximum of 127 characters)
        -f                      Forces running applications to close without war
ning
        -d [u][p]:xx:yy         The reason code for the shutdown
                                u is the user code
                                p is a planned shutdown code
                                xx is the major reason code (positive integer le
ss than 256)
                                yy is the minor reason code (positive integer le
ss than 65536)

-l would be logoff

I had an idea for a program and saw this. Thank you for the advice, it really helped; however, I am not sure how to implement it all...

... when using the following commands, what is the correct way to input it?

following the example code given earlier, I typed:
ProcessStartInfo startInfo = new ProcessStartInfo("shutdown.exe", "-m \\COMPUTERNAME");

upon clicking the button this code was written for, nothing happened on the computer I was trying to shut down, though (unfortunately) my computer restarted after approximately 30 seconds. I don't think I input it correctly, or I was attempting to do something -m wasn't meant to do?

Also, when I typed "-t 10" (wanting the computer to shut down 10 seconds after the button was clicked, nothing happened.

Wat am I doing wrong?
Thanks for the help
j4nu5

But why you use shutdown.exe?

There are API functions: ExitWindows, ExitWindowsEx, InitiateSystemShutdown, InitiateSystemShutdownEx. Try this.

I was trying to do it with the shutdown.exe because I did not know about this way.

For the API functions do I just need to be

using System;

or is there something else I would need to "use" in order for it to work.

thanks

Yes - API is the perfect way of doing it. Spawning a command prompt etc. eventually under Vista (UAC) will create a lot of undesirable results.

Look up DllImport attibute for calling APIs in .NET.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.