如何关闭计算机从C#

编程入门 行业动态 更新时间:2024-10-25 18:36:01
本文介绍了如何关闭计算机从C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

什么是关机的最好办法从C#程序的计算机?

What's the best way to shutdown 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 .

注意2015

在这个非常古老的问题:现代的解决方案,Windows8的+,简直是弥Vertal的下面

On this very old question: the modern solution, Windows8+, is simply Micah Vertal's below.

推荐答案

来自的一Geekpedia帖子

此方法使用 WMI 以关闭窗口。

This method uses WMI to shutdown windows.

您将需要一个参考System.Management添加到您的项目中使用此功能。

You'll need to add a reference to System.Management to your project to use this.

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#

本文发布于:2023-11-02 13:45:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552555.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:关闭计算机

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!