Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何关闭';允许混合睡眠';在高级电源设置中?由c#_C# - Fatal编程技术网

如何关闭';允许混合睡眠';在高级电源设置中?由c#

如何关闭';允许混合睡眠';在高级电源设置中?由c#,c#,C#,如何在高级电源设置中关闭“允许混合睡眠”?由c# 手动:电源选项->更改计划设置->更改高级电源设置-> Sleep->“Allow hybrid Sleep”->“Plugh-in:OFF使用procmon,我设法确定以下注册表项负责我的机器上的睡眠 HKEY\U LOCAL\U MACHINE\SYSTEM\CurrentControlSet\Control\Power\PowerSettings\238C9FA8-0AAD-41ED-83F4-97BE242C8F20\94ac6d29-7

如何在高级电源设置中关闭“允许混合睡眠”?由c# 手动:电源选项->更改计划设置->更改高级电源设置->
Sleep->“Allow hybrid Sleep”->“Plugh-in:OFF

使用procmon,我设法确定以下注册表项负责我的机器上的睡眠

HKEY\U LOCAL\U MACHINE\SYSTEM\CurrentControlSet\Control\Power\PowerSettings\238C9FA8-0AAD-41ED-83F4-97BE242C8F20\94ac6d29-73ce-41a6-809f-63BA21B47E

你可能需要对你的机器做一些研究,看看它是如何为你工作的

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

  • 电源配置文件的GUID
  • 配置文件子组的GUID
  • 设置的GUID
  • 您可以使用
    powercfg-QUERY
    生成完整的值列表

    获得要编辑的配置文件的GUID、子组的GUID(在本例中为睡眠子组)和设置的GUID(允许混合睡眠)后,您可以使用
    powercfg-SETACVALUEINDEX
    进行插入式设置,或使用
    powercfg-SETDCVALUEINDEX
    进行电池充电设置

    在我的情况下(Win7 Ultimate x64),您可以使用以下命令将其关闭:
    powercfg-SETACVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 238c9fa8-0aad-41ed-83f4-97be242c8f20 94ac6d29-73ce-41a6-809f-6363ba21b47e 1

    这将转换为中的AcSettingIndex值:

    HKEY\U LOCAL\U MACHINE\SYSTEM\CurrentControlSet\Control\Power\PowerSettings\238C9FA8-0AAD-41ED-83F4-97BE242C8F20\94AC6D29-73CE-41A6-809F-63BA21B47E\DefaultPowerSchemeValues\381b4222-f694-41f0-9685-ff5bb260df2e
    如果您的目标是Windows 7/2008服务器,则可以使用WMI和该类。下面是实现这一点的代码。确保使用指令向
    系统管理
    添加程序集引用和

        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;
        }
    

    请记住,“混合睡眠”在Windows7中只是“睡眠”,你确定吗?我与win7操作系统一起使用,因此看起来“允许混合睡眠”是的,电源选项在那里,但当你实际按下“睡眠”按钮时,它将根据电源选项进行正常或混合睡眠。这最好与组策略一起完成,操作注册表项或使用powercfg就可以了。电源管理api是解决问题的正确方法,但组策略听起来更合适effective@DavidHeffernan-我倾向于对一切进行逆向工程,只是为了实现它;)@大卫·费弗南——当然。我还没有接触过电源管理API,所以我想我应该了解控制面板和powercfg是如何做到这一点的。是否愿意发布一个答案,详细说明如何使用电源管理API实现此功能?很高兴我能提供一些帮助:)