如何从 C# 关闭计算机
Posted
技术标签:
【中文标题】如何从 C# 关闭计算机【英文标题】:How to shut down the computer from C# 【发布时间】:2010-09-11 06:45:00 【问题描述】:从 C# 程序关闭计算机的最佳方法是什么?
我找到了一些有效的方法 - 我将在下面发布它们 - 但它们都不是很优雅。我正在寻找更简单的原生 .net 的东西。
【问题讨论】:
【参考方案1】:从 windows XP 开始工作,在 win 2000 或更低版本中不可用:
这是最快的方法:
Process.Start("shutdown","/s /t 0");
否则像其他人所说的那样使用 P/Invoke 或 WMI。
编辑:如何避免创建窗口
var psi = new ProcessStartInfo("shutdown","/s /t 0");
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
Process.Start(psi);
【讨论】:
这似乎也适用于服务(至少在我关心的场景中)。我永远无法让 WMI 或 ExitWindowsEx 方法从服务中工作。 @James 这是因为服务通常没有权限。 使用这个后机器的耗电状态与使用传统的关机对话窗口后不同。按下电源按钮重新启动会消耗大约 80-85 毫安,而不是标准的 300+ish。如果我找出原因,会回到这里。这应该不会影响大多数用户。 这很好用,除了如果你在 WPF 中,这会在瞬间产生一个控制台窗口,看起来并不专业。【参考方案2】:取自:a Geekpedia post
此方法使用WMI 关闭窗口。
您需要在项目中添加对 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);
【讨论】:
使用 WMI 可以更轻松地跟踪错误。如果关闭命令由于某种原因不起作用会怎样? 我正在使用这种方法关闭窗口,三分之二它会告诉我我没有权限,但第三次,它有点“放弃”并重新启动计算机反正。那是怎么回事? 这个解决方案对我不起作用。即使我在管理员用户下运行程序,我也会收到“Privilege notheld”异常。 @roomaroo 这个方法不起作用。它抛出一个异常:管理异常,特权未持有。 如果你想强制关机,你应该使用 mboShutdownParams["Flags"] = "5";值 5 表示强制关闭。【参考方案3】:这个线程提供了必要的代码:http://bytes.com/forum/thread251367.html
但这里是相关代码:
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, Pack=1)]
internal struct TokPriv1Luid
public int Count;
public long Luid;
public int Attr;
[DllImport("kernel32.dll", ExactSpelling=true) ]
internal static extern IntPtr GetCurrentProcess();
[DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool OpenProcessToken( IntPtr h, int acc, ref IntPtr
phtok );
[DllImport("advapi32.dll", SetLastError=true) ]
internal static extern bool LookupPrivilegeValue( string host, string name,
ref long pluid );
[DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool AdjustTokenPrivileges( IntPtr htok, bool disall,
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen );
[DllImport("user32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool ExitWindowsEx( int flg, int rea );
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
internal const int EWX_LOGOFF = 0x00000000;
internal const int EWX_SHUTDOWN = 0x00000001;
internal const int EWX_REBOOT = 0x00000002;
internal const int EWX_FORCE = 0x00000004;
internal const int EWX_POWEROFF = 0x00000008;
internal const int EWX_FORCEIFHUNG = 0x00000010;
private void DoExitWin( int flg )
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
ok = OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok );
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tp.Luid );
ok = AdjustTokenPrivileges( htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero );
ok = ExitWindowsEx( flg, 0 );
用法:
DoExitWin( EWX_SHUTDOWN );
或
DoExitWin( EWX_REBOOT );
【讨论】:
您可以在此处了解其他 EWX_ 联系人的操作:msdn.microsoft.com/en-us/library/windows/desktop/… 将数字常量移植到 C# 时,最佳做法是使用枚举。这就是枚举的设计目的。它提供了围绕数字常量的强类型,可选地支持标志/位掩码,并且可以轻松地来回转换为基础数字类型。【参考方案4】:不同的方法:
A. System.Diagnostics.Process.Start("Shutdown", "-s -t 10");
B. Windows 管理规范 (WMI)
http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=36953 http://www.dreamincode.net/forums/showtopic33948.htmC. System.Runtime.InteropServices Pinvoke
http://bytes.com/groups/net-c/251367-shutdown-my-computer-using-cD.系统管理
http://www.geekpedia.com/code36_Shut-down-system-using-Csharp.html我提交后,我看到很多人也发了...
【讨论】:
B和D是同一种方法(WMI) E. Powershell从代码blogs.msdn.microsoft.com/kebab/2014/04/28/…执行脚本【参考方案5】:短而甜。调用外部程序:
using System.Diagnostics;
void Shutdown()
Process.Start("shutdown.exe", "-s -t 00");
注意:这会调用 Windows 的 Shutdown.exe 程序,因此它只有在该程序可用时才能工作。 您可能在 Windows 2000(其中 shutdown.exe 仅在资源工具包中可用)或XP Embedded 上遇到问题。
【讨论】:
【参考方案6】:老式的丑陋方法。使用 Win32 API 中的 ExitWindowsEx
函数。
using System.Runtime.InteropServices;
void Shutdown2()
const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
const short SE_PRIVILEGE_ENABLED = 2;
const uint EWX_SHUTDOWN = 1;
const short TOKEN_ADJUST_PRIVILEGES = 32;
const short TOKEN_QUERY = 8;
IntPtr hToken;
TOKEN_PRIVILEGES tkp;
// Get shutdown privileges...
OpenProcessToken(Process.GetCurrentProcess().Handle,
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, out hToken);
tkp.PrivilegeCount = 1;
tkp.Privileges.Attributes = SE_PRIVILEGE_ENABLED;
LookupPrivilegeValue("", SE_SHUTDOWN_NAME, out tkp.Privileges.pLuid);
AdjustTokenPrivileges(hToken, false, ref tkp, 0U, IntPtr.Zero,
IntPtr.Zero);
// Now we have the privileges, shutdown Windows
ExitWindowsEx(EWX_SHUTDOWN, 0);
// Structures needed for the API calls
private struct LUID
public int LowPart;
public int HighPart;
private struct LUID_AND_ATTRIBUTES
public LUID pLuid;
public int Attributes;
private struct TOKEN_PRIVILEGES
public int PrivilegeCount;
public LUID_AND_ATTRIBUTES Privileges;
[DllImport("advapi32.dll")]
static extern int OpenProcessToken(IntPtr ProcessHandle,
int DesiredAccess, out IntPtr TokenHandle);
[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AdjustTokenPrivileges(IntPtr TokenHandle,
[MarshalAs(UnmanagedType.Bool)]bool DisableAllPrivileges,
ref TOKEN_PRIVILEGES NewState,
UInt32 BufferLength,
IntPtr PreviousState,
IntPtr ReturnLength);
[DllImport("advapi32.dll")]
static extern int LookupPrivilegeValue(string lpSystemName,
string lpName, out LUID lpLuid);
[DllImport("user32.dll", SetLastError = true)]
static extern int ExitWindowsEx(uint uFlags, uint dwReason);
在生产代码中,您应该检查 API 调用的返回值,但为了使示例更清晰,我将其省略了。
【讨论】:
【参考方案7】:System.Diagnostics.Process.Start("shutdown", "/s /t 0")
应该可以。
要重启,是 /r
这将直接干净地重新启动 PC 盒,没有对话框。
【讨论】:
这是现代(2015+)系统的完美答案。 谢谢,你能解释一下 /s 和 /t 0 的作用吗? @Peterverleg 当然。 “/s”参数告诉计算机关闭,“/t”告诉计算机在关闭前等待 x 秒。我从个人经验中知道“/t”参数在 Windows 8.1 中没有任何作用,但它在 7 中确实有效。您也可以使用这些函数:shutdown /s /t 0 //For shutdown
shutdown /r /t 0 //For restart
shutdown /h /t 0 //For hibernate
另外,尝试将它们输入 CMD 以获得相同的结果。【参考方案8】:
只是为了补充 Pop Catalin 的答案,这里有一个 one liner,它会关闭计算机而不显示任何窗口:
Process.Start(new ProcessStartInfo("shutdown", "/s /t 0")
CreateNoWindow = true, UseShellExecute = false
);
【讨论】:
【参考方案9】:注意shutdown.exe
只是InitiateSystemShutdownEx
的包装,它提供了ExitWindowsEx
中缺少的一些细节
【讨论】:
【参考方案10】:您可以启动关机进程:
shutdown -s -t 0
- 关机
shutdown -r -t 0
- 重启
【讨论】:
【参考方案11】:我在尝试使用上面接受的 WMI 方法时遇到了麻烦,因为尽管以管理员身份运行程序,但我总是得到 privilige notheld 异常。
解决方案是让进程为自己请求权限。我在http://www.dotnet247.com/247reference/msgs/58/292150.aspx 找到了由一个叫 Richard Hill 的人写的答案。
我在下面粘贴了他的解决方案的基本用法,以防链接变旧。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Runtime.InteropServices;
using System.Security;
using System.Diagnostics;
namespace PowerControl
public class PowerControl_Main
public void Shutdown()
ManagementBaseObject mboShutdown = null;
ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
mcWin32.Get();
if (!TokenAdjuster.EnablePrivilege("SeShutdownPrivilege", true))
Console.WriteLine("Could not enable SeShutdownPrivilege");
else
Console.WriteLine("Enabled SeShutdownPrivilege");
// 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
mboShutdownParams["Flags"] = "1";
mboShutdownParams["Reserved"] = "0";
foreach (ManagementObject manObj in mcWin32.GetInstances())
try
mboShutdown = manObj.InvokeMethod("Win32Shutdown",
mboShutdownParams, null);
catch (ManagementException mex)
Console.WriteLine(mex.ToString());
Console.ReadKey();
public sealed class TokenAdjuster
// PInvoke stuff required to set/enable security privileges
[DllImport("advapi32", SetLastError = true),
SuppressUnmanagedCodeSecurityAttribute]
static extern int OpenProcessToken(
System.IntPtr ProcessHandle, // handle to process
int DesiredAccess, // desired access to process
ref IntPtr TokenHandle // handle to open access token
);
[DllImport("kernel32", SetLastError = true),
SuppressUnmanagedCodeSecurityAttribute]
static extern bool CloseHandle(IntPtr handle);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int AdjustTokenPrivileges(
IntPtr TokenHandle,
int DisableAllPrivileges,
IntPtr NewState,
int BufferLength,
IntPtr PreviousState,
ref int ReturnLength);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool LookupPrivilegeValue(
string lpSystemName,
string lpName,
ref LUID lpLuid);
[StructLayout(LayoutKind.Sequential)]
internal struct LUID
internal int LowPart;
internal int HighPart;
[StructLayout(LayoutKind.Sequential)]
struct LUID_AND_ATTRIBUTES
LUID Luid;
int Attributes;
[StructLayout(LayoutKind.Sequential)]
struct _PRIVILEGE_SET
int PrivilegeCount;
int Control;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] // ANYSIZE_ARRAY = 1
LUID_AND_ATTRIBUTES[] Privileges;
[StructLayout(LayoutKind.Sequential)]
internal struct TOKEN_PRIVILEGES
internal int PrivilegeCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
internal int[] Privileges;
const int SE_PRIVILEGE_ENABLED = 0x00000002;
const int TOKEN_ADJUST_PRIVILEGES = 0X00000020;
const int TOKEN_QUERY = 0X00000008;
const int TOKEN_ALL_ACCESS = 0X001f01ff;
const int PROCESS_QUERY_INFORMATION = 0X00000400;
public static bool EnablePrivilege(string lpszPrivilege, bool
bEnablePrivilege)
bool retval = false;
int ltkpOld = 0;
IntPtr hToken = IntPtr.Zero;
TOKEN_PRIVILEGES tkp = new TOKEN_PRIVILEGES();
tkp.Privileges = new int[3];
TOKEN_PRIVILEGES tkpOld = new TOKEN_PRIVILEGES();
tkpOld.Privileges = new int[3];
LUID tLUID = new LUID();
tkp.PrivilegeCount = 1;
if (bEnablePrivilege)
tkp.Privileges[2] = SE_PRIVILEGE_ENABLED;
else
tkp.Privileges[2] = 0;
if (LookupPrivilegeValue(null, lpszPrivilege, ref tLUID))
Process proc = Process.GetCurrentProcess();
if (proc.Handle != IntPtr.Zero)
if (OpenProcessToken(proc.Handle, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
ref hToken) != 0)
tkp.PrivilegeCount = 1;
tkp.Privileges[2] = SE_PRIVILEGE_ENABLED;
tkp.Privileges[1] = tLUID.HighPart;
tkp.Privileges[0] = tLUID.LowPart;
const int bufLength = 256;
IntPtr tu = Marshal.AllocHGlobal(bufLength);
Marshal.StructureToPtr(tkp, tu, true);
if (AdjustTokenPrivileges(hToken, 0, tu, bufLength, IntPtr.Zero, ref ltkpOld) != 0)
// successful AdjustTokenPrivileges doesn't mean privilege could be changed
if (Marshal.GetLastWin32Error() == 0)
retval = true; // Token changed
TOKEN_PRIVILEGES tokp = (TOKEN_PRIVILEGES)Marshal.PtrToStructure(tu,
typeof(TOKEN_PRIVILEGES));
Marshal.FreeHGlobal(tu);
if (hToken != IntPtr.Zero)
CloseHandle(hToken);
return retval;
【讨论】:
这行得通,虽然我不喜欢不知道为什么。老实说,我想知道我是否应该使用“关闭”命令...【参考方案12】:我尝试roomaroo's WMI method 关闭 Windows 2003 Server,但在我将 `[STAThread]'(即“Single Threaded Apartment”线程模型)添加到 Main() 声明之前它无法工作:
[STAThread]
public static void Main(string[] args)
Shutdown();
然后我尝试从一个线程关闭,为了让它工作,我还必须将线程的“Apartment State”设置为 STA:
using System.Management;
using System.Threading;
public static class Program
[STAThread]
public static void Main(string[] args)
Thread t = new Thread(new ThreadStart(Program.Shutdown));
t.SetApartmentState(ApartmentState.STA);
t.Start();
...
public static void Shutdown()
// roomaroo's code
我是 C# 菜鸟,所以我不完全确定 STA 线程在关闭系统方面的重要性(即使在阅读了我上面发布的链接之后)。也许其他人可以详细说明...?
【讨论】:
其实只有调用WMI的线程才需要是STA线程。如果那不是主线程,Main()
不需要[STAThread]
。【参考方案13】:
**详细答案...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
// Remember to add a reference to the System.Management assembly
using System.Management;
using System.Diagnostics;
namespace ShutDown
public partial class Form1 : Form
public Form1()
InitializeComponent();
private void btnShutDown_Click(object sender, EventArgs e)
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
mboShutdownParams["Flags"] = "1";
mboShutdownParams["Reserved"] = "0";
foreach (ManagementObject manObj in mcWin32.GetInstances())
mboShutdown = manObj.InvokeMethod("Win32Shutdown", mboShutdownParams, null);
【讨论】:
【参考方案14】:使用shutdown.exe。为了避免传递参数的问题,复杂的执行,从WindowForms执行使用PowerShell执行脚本:
using System.Management.Automation;
...
using (PowerShell PowerShellInstance = PowerShell.Create())
PowerShellInstance.AddScript("shutdown -a; shutdown -r -t 100;");
// invoke execution on the pipeline (collecting output)
Collection<PSObject> PSOutput = PowerShellInstance.Invoke();
System.Management.Automation.dll 应安装在操作系统上并在 GAC 中可用。
对不起我的英语。
【讨论】:
【参考方案15】:对于 Windows 10,我需要添加 /f 选项以便在没有任何问题和等待时间的情况下关闭电脑。
//This did not work for me
Process.Start("shutdown", "/s /t 0");
//But this worked
Process.Start("shutdown", "/s /f /t 0");
【讨论】:
【参考方案16】:没有关闭计算机的 .net 本地方法。您需要 P/Invoke ExitWindows 或 ExitWindowsEx API 调用。
【讨论】:
【参考方案17】:如果你想远程关闭计算机,那么你可以使用
Using System.Diagnostics;
点击任意按钮
Process.Start("Shutdown","-i");
【讨论】:
以上是关于如何从 C# 关闭计算机的主要内容,如果未能解决你的问题,请参考以下文章