如何在高级电源设置中关闭“允许混合睡眠”?通过c#

时间:2011-10-05 12:31:50

标签: c#

如何在高级电源设置中关闭“允许混合睡眠”?通过c# 通过手动:电源选项 - >更改计划设置 - >改变高级功率设置 - > 睡眠 - > '允许混合睡眠' - >插入:关闭

2 个答案:

答案 0 :(得分:2)

如果您的目标是Windows 7/2008 Server,则可以使用WMI和Win32_PowerSetting类。下面是执行此操作的代码。确保将程序集引用和using指令添加到System.Management

    private bool SetAllowHybridSleep(bool enabled)
    {
        //Machine to work on, "." for local
        string RemotePC = ".";

        //Set the namespace to the power namespace, used throughout the function
        ManagementScope ms = new ManagementScope(@"\\" + RemotePC + @"\root\cimv2\power");

        //Will hold each of our queries
        ObjectQuery oq = null;

        //Will hold the values of our power plan and the specific setting that we want to change
        Guid PowerPlanInstanceId = Guid.Empty;
        string PowerSettingInstanceId = null;

        //Look for the specific setting that we want
        oq = new ObjectQuery(string.Format("SELECT * FROM Win32_PowerSetting WHERE ElementName = 'Allow hybrid sleep'"));
        using (ManagementObjectSearcher mos = new ManagementObjectSearcher(ms, oq))
        {
            ManagementObjectCollection results = mos.Get();
            foreach (ManagementObject obj in results)
            {
                foreach (PropertyData p in obj.Properties)
                {
                    if (p.Name == "InstanceID")
                    {
                        //This will give us a string with a GUID specific to our setting
                        PowerSettingInstanceId = p.Value.ToString();
                        break;
                    }
                }
            }
        }

        //Sanity check
        if (string.IsNullOrEmpty(PowerSettingInstanceId))
        {
            Console.WriteLine("System does not support hybrid sleep");
            return false;
        }

        //Look for the active power scheme
        oq = new ObjectQuery("SELECT * FROM Win32_PowerPlan WHERE IsActive=True");
        using (ManagementObjectSearcher mos = new ManagementObjectSearcher(ms, oq))
        {
            ManagementObjectCollection results = mos.Get();
            foreach (ManagementObject obj in results)
            {
                foreach (PropertyData p in obj.Properties)
                {
                    if (p.Name == "InstanceID")
                    {
                        //The instance contains a string with a GUID inside of it, use the code below to get the GUID by itself
                        if (!Guid.TryParse(System.Text.RegularExpressions.Regex.Match(p.Value.ToString(), @"\{[0-9a-f]{8}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{12}\}").Value, out PowerPlanInstanceId))
                        {
                            Console.WriteLine("Could not find active power plan");
                            return false;
                        }
                        break;
                    }
                }
            }
        }

        //Now we need to update the actual power setting in the active plan
        //Get all power schemes for the target setting
        oq = new ObjectQuery(string.Format("ASSOCIATORS OF {{Win32_PowerSetting.InstanceID=\"{0}\"}} WHERE ResultClass = Win32_PowerSettingDataIndex", PowerSettingInstanceId.Replace(@"\", @"\\")));
        using (ManagementObjectSearcher mos = new ManagementObjectSearcher(ms, oq))
        {
            ManagementObjectCollection results = mos.Get();
            foreach (ManagementObject obj in results)
            {
                foreach (PropertyData p in obj.Properties)
                {
                    //See if the current scheme is the current setting. This will happen twice, once for AC and once for DC
                    if (p.Name == "InstanceID" && p.Value.ToString().Contains(PowerPlanInstanceId.ToString()))
                    {
                        //Change the value of the current setting
                        obj.SetPropertyValue("SettingIndexValue", (enabled ? "1" : "0"));
                        obj.Put();
                        break;
                    }
                }
            }
        }

        return true;
    }

答案 1 :(得分:0)

使用procmon,我设法计算出以下注册表项在我的机器上负责。

HKEY_LOCAL_MACHINE \ SYSTEM \ CURRENTCONTROLSET \控制\电源\ PowerSettings \ 238C9FA8-0AAD-41ED-83F4-97BE242C8F20 \ 94ac6d29-73ce-41a6-809f-6363ba21b47e

您可能需要对您的计算机进行一些研究,以了解它是如何运作的。

您也可以调用powercfg实用程序来执行此操作。每个功率设置由三个部分标识:

  1. 电力配置文件的GUID
  2. 个人资料小组的GUID
  3. 设置指南
  4. 您可以使用powercfg -QUERY生成完整的值列表。

    一旦获得了要编辑的配置文件的GUID,子组的GUID(在本例中为Sleep子组)和设置的GUID(允许混合睡眠),您可以使用{{1}插入或powercfg -SETACVALUEINDEX用于电池设置值。

    在我的情况下(Win7 Ultimate x64)你可以使用以下方式将其关闭:
    powercfg -SETDCVALUEINDEX

    这转换为AcSettingIndex值:
    powercfg -SETACVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 238c9fa8-0aad-41ed-83f4-97be242c8f20 94ac6d29-73ce-41a6-809f-6363ba21b47e 1