如何从C#关闭计算机 [英] How to shut down the computer from C#
本文介绍了如何从C#关闭计算机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从C#程序关闭计算机的最佳方法是什么?
What's the best way to shut down the computer from a C# program?
我找到了一些可行的方法-我将其发布在下面-但他们都不是很优雅。我正在寻找更简单的本机.net。
I've found a few methods that work - I'll post them below - but none of them are very elegant. I'm looking for something that's simpler and natively .net.
推荐答案
来自: Geekpedia帖子
此方法使用< a href = http://en.wikipedia.org/wiki/Windows_Management_Instrumentation rel = noreferrer> WMI 关闭窗口。
您
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);
}
}
这篇关于如何从C#关闭计算机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文