从C#程序关闭计算机的最佳方法是什么?
我发现了一些可行的方法-我将在下面发布它们-但它们都不是非常优雅。我正在寻找更简单的本机.net。
摘自:Geekpedia帖子
此方法使用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); } }