我正在尝试用 VB.NET 编写一个小程序,它可以检测我的笔记本电脑何时与电源断开连接,并在插入电源时将电源计划切换到省电模式,反之亦然。
我尝试通过注册表使用此代码片段进行操作
Select Case power_status.ACLineStatus
Case 0
Dim CurrentPowerPlan As String = My.Computer.Registry.LocalMachine.OpenSubKey("SYSTEM").OpenSubKey("CurrentControlSet").OpenSubKey("Control").OpenSubKey("Power").OpenSubKey("User").OpenSubKey("PowerSchemes").GetValue("ActivePowerScheme")
If CurrentPowerPlan <> "a1841308-3541-4fab-bc81-f71556f20b4a" Then
Label1.Text = "Running On Battery"
ChangePowerPlan("a1841308-3541-4fab-bc81-f71556f20b4a") 'Power Saver
End If
Case 1
Dim CurrentPowerPlan As String = My.Computer.Registry.LocalMachine.OpenSubKey("SYSTEM").OpenSubKey("CurrentControlSet").OpenSubKey("Control").OpenSubKey("Power").OpenSubKey("User").OpenSubKey("PowerSchemes").GetValue("ActivePowerScheme")
If CurrentPowerPlan <> "8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c" Then
Label1.Text = "Connected To NEPA"
ChangePowerPlan("8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c") 'High Performance
End If
'Case 255
'MessageBox.Show("Unknown")
End Select
但它又抛出了这个错误
Requested registry access is not allowed.
我也听说过 Win32 函数“PowerSetActiveScheme”,但我不知道如何将它映射到 VB.NET。