开发者

how can I turn off 'allow hybrid sleep' in advanced power setting? by c#

how can I turn off 'allow hybrid sleep' in advanced power setting? by c# by manually: power options -> change Plan S开发者_Python百科ettings -> change advanced power setting-> Sleep-> 'Allow hybrid sleep' -> plugged in: OFF


If you are targeting Windows 7/2008 Server then you can use WMI and the Win32_PowerSetting class. Below is code that does that. Make sure to add an assembly reference and using directive to 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;
    }


Using procmon, I managed to work out that the following registry key is responsible for it on my machine.

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\PowerSettings\238C9FA8-0AAD-41ED-83F4-97BE242C8F20\94ac6d29-73ce-41a6-809f-6363ba21b47e

You'll probably have to do some research on your machine to see how it works for you.

You can also call on the powercfg utility to do this. Each power setting is identified by three things:

  1. GUID of power profile
  2. GUID of profile subgroup
  3. GUID of setting

You can use powercfg -QUERY to produce a full list of values.

Once you've got the GUID of the profile you want to edit, the GUID of the subgroup (in this case the Sleep subgroup) and the GUID of the setting (Allow Hybrid Sleep) you can use either powercfg -SETACVALUEINDEX for plugged in or powercfg -SETDCVALUEINDEX for on battery to set the value.

In my case (Win7 Ultimate x64) you can turn it off using:
powercfg -SETACVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 238c9fa8-0aad-41ed-83f4-97be242c8f20 94ac6d29-73ce-41a6-809f-6363ba21b47e 1

This translates to the AcSettingIndex value in:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\PowerSettings\238C9FA8-0AAD-41ED-83F4-97BE242C8F20\94AC6D29-73CE-41A6-809F-6363BA21B47E\DefaultPowerSchemeValues\381b4222-f694-41f0-9685-ff5bb260df2e

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜