Vb.net – Access to the registry key ‘[KEY_NAME]’ is denied

registryunauthorizedaccessexceptivb.net

I'm writing a small program in Visual Basic 2008 that flips the values of specific DWORDs in a registry key

The registry key in question is:

'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Render\{91801674-82d9-459a-9358-6e5cf3d81d21}\FxProperties'

The dword I'm manipulating is "{e0a941a0-88a2-4df5-8d6b-dd20bb06e8fb},4"

This is the line of code I wrote to set the DWORD's value is this:

Dim keyString = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Render\{91801674-82d9-459a-9358-6e5cf3d81d21}\FxProperties"
My.Computer.Registry.SetValue(keyString, "{ad75efc0-8f48-4285-bfa8-40fb036cdab2},2", "00000000")

But I get a UnauthorizedAccessException at runtime stating that "Access to the registry key [KEY_NAME] is denied."

I ran the program with Administrator privileges, changed the app's manifest to include:

<requestedExecutionLevel level="highestAvailable" uiAccess="false" />

But that didn't work either. So I searched a few forums and tried this:

Dim rkLM As RegistryKey = Registry.LocalMachine
Dim pRegKey As RegistryKey = rkLM.OpenSubKey("\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Render\{91801674-82d9-459a-9358-6e5cf3d81d21}\FxProperties", True)
pRegKey.SetValue("{ad75efc0-8f48-4285-bfa8-40fb036cdab2},2", "00000000")

But that threw a NullReferenceException at me stating "Object reference not set to an instance of an object."

Is there any way I can modify that that key without having to run my program with SYSTEM privileges?

Best Answer

You should probably try with requireAdministrator in your manifest because highestAvailable may not actually be an administrator.

I would also try specifying the data type (in your case I think it is binary):

My.Computer.Registry.SetValue(keyString, _
"{ad75efc0-8f48-4285-bfa8-40fb036cdab2},2", _ 
"00000000", _
RegistryValueKind.Binary)

However the value you are setting may need to be a byte array (something else you could try)

Related Topic