Shoutbox

Visual Basic .NET: Deleting a Registry Value failed - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Visual Basic .NET: Deleting a Registry Value failed (/showthread.php?tid=84790)

Visual Basic .NET: Deleting a Registry Value failed by ryxdp on 07-12-2008 at 08:21 AM

This is what I've got:

vbnet code:
My.Computer.Registry.CurrentUser.DeleteValue("HKEY_CURRENT_USER\Control Panel\Desktop\testingval\")

But it's telling me the value doesn't exist, when it clearly does. Why is this? I've tried stripping the HKEY_CURRENT_USER off it, to no avail, it still thinks the value isn't there. I must be pretty blind to see through it; what am I doing wrong?

Thanks in advance =)
RE: Visual Basic .NET: Deleting a Registry Value failed by Matti on 07-12-2008 at 10:09 AM

I don't have much (read: as good as no) experience with VB.NET, but here are my thoughts:

If "testingkey" is a registry key, you shouldn't be using DeleteValue but DeleteSubKeyTree.
The HKEY_CURRENT_USER should also be left out, since you're already in the CurrentUser key.


RE: RE: Visual Basic .NET: Deleting a Registry Value failed by ryxdp on 07-12-2008 at 10:16 AM

quote:
Originally posted by Mattike
I don't have much (read: as good as no) experience with VB.NET, but here are my thoughts:

If "testingkey" is a registry key, you shouldn't be using DeleteValue but DeleteSubKeyTree.
The HKEY_CURRENT_USER should also be left out, since you're already in the CurrentUser key.

hmm...I'm not thinking straight..."testingkey" should be "testingval" instead. And yes, I've tried removing the HKEY_CURRENT_USER but it doesn't appear to do anything helpful to me anyway.
RE: Visual Basic .NET: Deleting a Registry Value failed by CookieRevised on 07-12-2008 at 02:55 PM

I don't have much (read: as good as no) experience with VB.NET either, but here are my thoughts (taken from the MSDN library):

quote:
Originally posted by ryxdp
code:
My.Computer.Registry.CurrentUser.DeleteValue("HKEY_CURRENT_USER\Control Panel\Desktop\testingval\")

for starters, is My.Computer.Registry.CurrentUser defined as a proper RegistryKey object?

Second, if "testingval" is a value, you should not add the last backslash (you shouldn't even add it if it is a subkey, I think).

Second, you need to open that key first before you can delelete the value "testingval"...

See DeleteValue

eg:

' Creates a subkey under HKCU named "Test9999"
Dim test9999 As RegistryKey = Registry.CurrentUser.CreateSubKey("Test9999")
' Thus not:
' Registry.CurrentUser.CreateSubKey("HKEY_CURRENT_USER\Test9999\") I think


Dim testSettings As RegistryKey = test9999.CreateSubKey("TestSettings")
testSettings.SetValue("ID", 123)
testSettings.Close()

' Deletes the newly created "id" value under the "HKCU\Test9999\TestSettings\" key. Assuming test9999 is a proper defined RegistryKey object.
testSettings = test9999.OpenSubKey("TestSettings", True)
testSettings.DeleteValue("id")
testSettings.Close()

Full example code is given on the MSDN library::RegistryKey Class
RE: Visual Basic .NET: Deleting a Registry Value failed by ryxdp on 07-13-2008 at 12:26 AM

It still thinks the value doesn't exist. I must have missed something in the code you gave me, Cookie.

Here is the full Sub:

vbnet code:
    Private Sub NotifyIcon1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrayIcon.DoubleClick

        'If screensaver is active on double click, delete the SCRNSAVE.EXE value and change ScreenSaveActive to 0
        If My.Computer.Registry.GetValue("HKEY_Current_User\Control Panel\Desktop\", "ScreenSaveActive", "Unknown") = "1" Then
            My.Computer.Registry.CurrentUser.OpenSubKey("HKEY_CURRENT_USER\Control Panel\Desktop", True)
            My.Computer.Registry.CurrentUser.DeleteValue("testingval")
            My.Computer.Registry.CurrentUser.Close()
            My.Computer.Registry.SetValue("HKEY_Current_User\Control Panel\Desktop\", "ScreenSaveActive", "0")
        End If
        If My.Computer.Registry.GetValue("HKEY_Current_User\Control Panel\Desktop\", "ScreenSaveActive", "Unknown") = "0" Then

            'Otherwise create the SCRNSAVE.EXE value, using the data from conf.ini and change ScreenSaveActive to 1
            My.Computer.Registry.SetValue("HKEY_Current_User\Control Panel\Desktop\", "testingkey", "C:\Windows\System32\ribbons.scr")
            My.Computer.Registry.SetValue("HKEY_Current_User\Control Panel\Desktop\", "ScreenSaveActive", "1")
        End If
    End Sub

RE: RE: Visual Basic .NET: Deleting a Registry Value failed by pollolibredegrasa on 07-13-2008 at 01:48 AM

This code works for me, and should get you started...

code:
Dim regkey As RegistryKey = My.Computer.Registry.CurrentUser.OpenSubKey("Control Panel\Desktop", True)

You can then go on and use stuff like
code:
Dim svalue As String = regkey.GetValue("ScreenSaveActive")
regkey.DeleteValue("testingval")

etc...

A couple of other things I've noticed.

First,
code:
My.Computer.Registry.CurrentUser.OpenSubKey("HKEY_CURRENT_USER\Control Panel\Desktop", True)
Here you've already opened HKEY_CURRENT_USER, by stating it again you make it look for a subkey called HKEY_CURRENT_USER, which is one reason this line probably doesn't work.


Secondly, you used 2 If statements in a row:
code:
If <snip>
            My.Computer.Registry.SetValue("HKEY_Current_User\Control Panel\Desktop\", "ScreenSaveActive", "0")
End If

If My.Computer.Registry.GetValue("HKEY_Current_User\Control Panel\Desktop\", "ScreenSaveActive", "Unknown") = "0" Then
If your code worked and changed the correct values, your first If statement would change a value to 0. Immediately after that your second If statement would also be executed and change it back to 1 (because the value is now 0). You should change to an If..Else statement or add an Exit Sub in your Ifs to avoid this.

Hope this helps :)
RE: Visual Basic .NET: Deleting a Registry Value failed by ryxdp on 07-13-2008 at 05:11 AM

Thanks, it works perfectly now. I did originally have an Else in there, but I don't know why I removed it...

Anyway here is the full working code for anyone else stuck on this (doubtful, but who knows :P)

vbnet code:
    Private Sub TrayIcon_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrayIcon.DoubleClick
        'If screensaver is active on double click, delete the SCRNSAVE.EXE value and change ScreenSaveActive to 0
        If My.Computer.Registry.GetValue(regkey.ToString, "ScreenSaveActive", "Unknown") = "1" Then
            regkey.DeleteValue("SCRNSAVE.EXE")
            My.Computer.Registry.SetValue("HKEY_Current_User\Control Panel\Desktop\", "ScreenSaveActive", "0")
            TrayIcon.Text = My.Resources.inactive
        Else
            'Otherwise create the SCRNSAVE.EXE value and change ScreenSaveActive to 1
            My.Computer.Registry.SetValue("HKEY_Current_User\Control Panel\Desktop\", "SCRNSAVE.EXE", "C:\Windows\System32\ribbons.scr")
            My.Computer.Registry.SetValue("HKEY_Current_User\Control Panel\Desktop\", "ScreenSaveActive", "1")
            TrayIcon.Text = My.Resources.active
        End If
    End Sub