如何在高级电源设置中关闭'允许混合睡眠' ?由c#手动:电源选项->更改计划设置->更改高级电源设置->Sleep-> '允许混合睡眠' ->插入:OFF
如果你的目标是Windows 7/2008服务器,那么你可以使用WMI和Win32_PowerSetting类。下面是这样做的代码。确保在System.Management
中添加一个程序集引用和using
指令。
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 + @"rootcimv2power");
//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;
}
使用procmon,我设法找出了以下注册表项在我的机器上负责。
HKEY_LOCAL_MACHINE SYSTEM CurrentControlSet 控制 PowerSettings 238年c9fa8 - 0 -广告- 41 - ed - 83 - f4 - 97 be242c8f20 94 ac6d29 - 73 - ce - 41 - a6 - 809 f - 6363 ba21b47e
你可能需要对你的机器做一些研究,看看它是如何为你工作的。
您也可以调用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_LOCAL_MACHINESYSTEMCurrentControlSetControlPowerPowerSettings238C9FA8-0AAD-41ED-83F4-97BE242C8F2094AC6D29-73CE-41A6-809F-6363BA21B47EDefaultPowerSchemeValues381b4222-f694-41f0-9685-ff5bb260df2e