Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am trying to write a small program in VB.NET that detects when my laptop has been disconnected from the power mains and switches the power plan to power saver and vice versa when it is plugged into the mains.

I tried doing it through the registry with this code snippet

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

But it throws back this error

Requested registry access is not allowed.

I also have heard of a Win32 function `"PowerSetActiveScheme" but i don't know how to map it to VB.NET.

share|improve this question

1 Answer 1

You could try running a shell command from your VB.NET application to shell a command similar to the following:

powercfg -SETACTIVE {guidScheme2}

As per the information in this article about Power Schemes

share|improve this answer
    
How exactly do i run a shell command from my VB.NET application? Can you show me a code snippet, please –  Bezaleel Oct 26 '11 at 13:00
    
@Bezaleel: LMGTFY. Set oShell = WScript.CreateObject ("WScript.Shell") oShell.run "cmd /K CD C:\ & Dir" –  surfasb Oct 27 '11 at 3:26
    
Depending on which version of .NET you are using this article is probably a good place to start: msdn.microsoft.com/en-us/library/xe736fyk(v=vs.71).aspx –  Richard Oct 27 '11 at 6:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.