Error on deleting registry value

B

Brian8568

I'm doing something wrong. I'm trying to delete a registry value with
the following code VB 2005 Express:

Dim oReg As Microsoft.Win32.RegistryKey
oReg =
My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run")
Try
oReg.DeleteValue("AlcWzrd")
Debug.Print("Successfully Delete Registry Key AlcWzrd")
Catch ex As Exception
Debug.Print(ex.ToString)
Finally
oReg.Close()
End Try


This fails with:

A first chance exception of type 'System.UnauthorizedAccessException'
occurred in mscorlib.dll.
Cannot write to the registry key.

I'm running this under an admin account and I can add and remove this
key in RegEdit. What am I doing wrong?
 
B

Brian8568

I've tried both ways.
My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run")
doesn't work either.
 
F

FishingScout

That OpenSubKey opens the key as read-only.

Try this:

My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run",
True )
 
F

FishingScout

In addition, I think you should call DeleteSubKey rather than
DeleteValue to delete a key.
 
B

Brian8568

Bingo! Thank you.

That OpenSubKey opens the key as read-only.

Try this:

My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run",
True )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top