Search code examples
c#shutdown

C# Using Shutdown.exe just Shutdown


am using Shutdown.exe in my application because it offers some nice features like it can delay a shutdown and add a comment why need to shutdown.

The app specs can Shutdown, Restart, Log-Off, Hibernate, the Hybrid shutdown stuff, add comment, and delay a shutdown, and abort a scheduled shutdown.

The application was nice and dandy and I have made a setup file. I now tried installing it locally and used it. But when I tried for example clicked hybernate, it just shutdown ... I clicked on restart, it shuts down, and same thing with others.

I am pretty sure that I am using the correct combination of paramaters

this is the parameter I used

void PowerButtonsClick(object sender, RoutedEventArgs e)
{
    string p = string.Empty;

    if (sender == btnShutdown)
    {
        p += "-s";
    }
    else if (sender == btnRestart)
    {
        p += "-r";
    }
    else if (sender == btnSignoff)
    {
        p += "-l";
    }
    else if (sender == btnHibernate)
    {
        p += "-h";
    }
    else if (sender == btnHybridShutdown)
    {
        p += "-hybrid -s";
    }
    else if (sender == btnAbortShutdown)
    {
        p += "-a";
    }

    if (sender != btnAbortShutdown)
    {
        if (sender != btnSignoff || sender != btnHibernate)
        {
            if (isForced) { p += " -f"; }

            double seconds = TimeSpan.FromTicks(timePicker.Value.Value.Subtract(DateTime.Now).Ticks).TotalSeconds;

            p += " -t " + Convert.ToInt32(seconds);

            if (hasComment)
            {
                p += string.Format(" -c \"{0}\"", borderComment_txComment.Text);
            }
        }
    }

    System.Diagnostics.Debug.WriteLine("param: " + p);

    System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo()
    {
        Arguments = p,
        FileName = "shutdown.exe"
    });
}

and this is the application I created 3 days ago. It was meant for Windows 8, and now, I shut off the download link because of the problem http://wall.jaysonragasa.net/wall/post/2012/06/17/Windows-8-System-Power-Shortcuts.aspx

to tell you honestly, everything works well when running from IDE.. but when installed .. I had the problem. I even tried to disassemble the deployed application using ILSpy and it looks ok and nothing wrong..

-- UPDATE -- just to update my code in

if (sender != btnSignoff || sender != btnHibernate) { ~~ }

it must be

if (sender != btnSignoff && sender != btnHibernate) { ~~ }

the log result in case you requested

param: -s -f -t 0
param: -l
param: -r -f -t 0
param: -h
param: -a
param: -hybrid -s -f -t 0
param: -s -f -t 0 -c "my comment"
param: -s -f -t 5 -c "my comment"
param: -s -f -t 12118 -c "my comment"
param: -s -t 12110 -c "my comment"

-- UPDATE --

I have decided to use API calls but I loose the Abort functionality which is so important. If you can share how to Abort a scheduled shutdown, even if using WMI or API, kindly please share it.

Regards to all


Solution

  • I have used this before and it may work for you:

        private void ShutdownComputer(bool restart)
        {
            ManagementBaseObject mboShutdown = null;
            ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
            mcWin32.Get();        // You can't shutdown without security privileges
            mcWin32.Scope.Options.EnablePrivileges = true;
            ManagementBaseObject mboShutdownParams = mcWin32.GetMethodParameters("Win32Shutdown");
    
            // Flag 1 means we want to shut down the system. Use "2" to reboot.
            mboShutdownParams["Flags"] = restart ? "2" : "1";
            mboShutdownParams["Reserved"] = "0";
            foreach (ManagementObject manObj in mcWin32.GetInstances())
            {
                mboShutdown = manObj.InvokeMethod("Win32Shutdown", mboShutdownParams, null);
            }
        }