问题
I need to allow WakeTimers (computer wake up from sleep/hibernation) for all power plans set on plugged in to Enabled.
I tried Win32_PowerSetting but it only works on english version of Windows.
I need to use .NET 2.0
Thanks for responses !
回答1:
I suspect you can do this using API calls to powrprof.dll, as well as WMI, but I haven't had the time to figure that approach out.
This setting appears to be simply a boolean registry key that is located according to your current power plan:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\User\PowerSchemes\(Current Power Scheme GUID)\(Sleep Category GUID)\(Enable AC or DC Wake Timers GUID) = 0 or 1
Rather than manipulating the registry directly a cleaner approach would be to enable these settings using powercfg.exe.
For AC power:
powercfg.exe -SETACVALUEINDEX SCHEME_CURRENT SUB_SLEEP bd3b718a-0680-4d9d-8ab2-e1d2b4ac806d 1
For Batteries:
powercfg.exe -SETDCVALUEINDEX SCHEME_CURRENT SUB_SLEEP bd3b718a-0680-4d9d-8ab2-e1d2b4ac806d 1
EDIT:
This enables wake timers on my system when running on AC power using High Performance power scheme (purely as a proof of concept):
[DllImport("powrprof.dll", EntryPoint = "PowerWriteACValueIndex", CharSet = CharSet.Auto, SetLastError = true)]
public static extern uint PowerWriteACValueIndex(IntPtr RootPowerKey, ref Guid SchemeGuid, ref Guid SubGroupOfPowerSettingsGuid, ref Guid PowerSettingGuid, uint AcValueIndex);
public static void EnableWakeTimers()
{
Guid Root = new Guid("8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c"); // High Performance GUID
Guid Sleep = new Guid("238c9fa8-0aad-41ed-83f4-97be242c8f20"); // Sleep Subcategory GUID
Guid WakeTimers = new Guid("bd3b718a-0680-4d9d-8ab2-e1d2b4ac806d"); // Wake Timers GUID
PowerWriteACValueIndex(IntPtr.Zero, ref Root, ref Sleep, ref WakeTimers, 1);
}
This reference is your friend: http://msdn.microsoft.com/en-us/library/aa373163%28v=vs.85%29.aspx
回答2:
I wrote this powershell script to Enable or Disable wake timers on all current power schemes. On the second last line "POWERCFG -setacvalueindex $xAll $xSubGuid $zz 1" - the 1 means enable. Just change this to 0 to disable.
CLS
#Capture Current Active Power Scheme
$orgScheme = POWERCFG -GETACTIVESCHEME
$yOrg = $orgScheme -split "\s+"
$xOrg = $yOrg[3]
Write-host Original Scheme = $xOrg
Echo __________________________________
Echo " "
$allScheme = POWERCFG /L
#Echo $allScheme
foreach ($line in $allScheme)
{
if ($line.Length -gt 40)
{
if ($line.substring(0,5) -eq "Power")
{
$yAll = $line -split "\s+"
$xAll = $yAll[3]
write-host $xAll
Powercfg -S $xAll
$pScheme = POWERCFG /Q
foreach ($line in $pScheme)
{
$yy = $line -split "\s+"
$xx = $yy[5]+$yy[6]+$yy[7]+$yy[8]
$zz = $yy[4]
$xSubGroup = $yy[1]
If($xSubGroup -eq "SubGroup")
{
$xSubGuid = $yy[3]
}
If($xx -eq "(allowwaketimers)")
{
write-host Power Scheme Guid = $xAll
write-host Subgroup Guid = $xSubGuid
write-host WakeUp Guid = $zz
Write-host POWERCFG -setacvalueindex $x $xSubGuid $zz 1
Echo " "
break
}
}
POWERCFG -setacvalueindex $xAll $xSubGuid $zz 1
}
}
}
Powercfg -S $xOrg
Enjoy.
回答3:
It appears that you can use WMI to change the setting.
http://www.daniweb.com/software-development/csharp/threads/272577
Microsoft has a WMI code creator that will help you out:
https://www.microsoft.com/download/en/confirmation.aspx?displayLang=en&id=8572
You will want to search under "root\CIMV2\power" for the power management stuff.
回答4:
From https://adameyob.com/2015/02/how-to-enable-wake-timers/#comment-17
powercfg /SETACINDEXVALUE SCHEME_BALANCED SUB_SLEEP RTCWake 1
powercfg /SETACINDEXVALUE SCHEME_MIN SUB_SLEEP RTCWake 1
powercfg /SETACINDEXVALUE SCHEME_MAX SUB_SLEEP RTCWake 1
Or from http://adameyob.com/2015/02/how-to-enable-wake-timers/#comment-31
FOR /f “tokens=1,2,3,4” %%I IN (‘powercfg.exe /getactivescheme’) DO ( SET GUID=%%L )
powercfg /setacvalueindex %GUID% SUB_SLEEP RTCWAKE 1
来源:https://stackoverflow.com/questions/8501109/allow-wake-timers-windows-7-vista