3

我做了一些谷歌搜索,似乎总是回到相同的解决方案,这似乎不起作用!

private void btnRestart_Click(object sender, EventArgs e)
{
    System.Diagnostics.Process.Start("Shutdown.exe", "/r /f /t 00");
}

private void btnShutdown_Click(object sender, EventArgs e)
{
    System.Diagnostics.Process.Start("Shutdown.exe", "/s /f /t 00");
}

CMD 出现一秒钟,然后关闭,没有做任何事情。我错过了什么吗?

4

5 回答 5

1

这有点干净

using System.Runtime.InteropServices;

[DllImport("user32.dll", SetLastError = true)]
static extern int ExitWindowsEx(uint uFlags, uint dwReason);

ExitWindowsEx(1, 0); //this will cause the system to shut down.

ExitWindowsEx(2, 0); //this will cause the system to reboot.

于 2015-06-09T18:15:09.920 回答
0

这个答案是我从这里得到的。它对我有用。确保添加对System.Management

 using System.Management;

    void Shutdown()
    {
        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"] = "1";
        mboShutdownParams["Reserved"] = "0";
        foreach (ManagementObject manObj in mcWin32.GetInstances())
        {
            mboShutdown = manObj.InvokeMethod("Win32Shutdown", 
                                           mboShutdownParams, null);
        }
    }
于 2013-09-15T11:58:29.517 回答
0

将这些命名空间添加到您的代码中

    using System.Diagnostics;
    using System.Runtime.InteropServices;

甚至这取决于您被分配了哪些权限。我希望这能帮到您。

于 2013-09-15T10:41:28.597 回答
0

根据shutdown /?,您不使用斜杠 (/) 而是使用破折号 (-)。

或者,尝试cmd.exe使用参数运行/c shutdown.exe /r /f /t 00

于 2013-09-15T10:42:08.720 回答
0

尝试使用ProcessStartInfo 传递参数:

ProcessStartInfo startInfo = new ProcessStartInfo("shutdown.exe");
startInfo.Arguments = "/r /f /t 00";
Process.Start(startInfo);
于 2013-09-15T11:02:28.460 回答