以编程方式关闭计算机 [英] Programmatically shutdown the computer

查看:147
本文介绍了以编程方式关闭计算机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用C#关闭计算机?

How can I shutdown my computer using C#?

推荐答案

您好,

有几种可能,这是我能想到的2种:

1.使用WMI:
Hi,

there are several possibilities, here are 2 I can think of:

1. using 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);
        }
    }



2.使用过程:



2. using Process:

System.Diagnostics.Process.Start("shutdown","/s /t 0");


这篇关于以编程方式关闭计算机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆