What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » Visual Basic .NET: Deleting a Registry Value failed

Visual Basic .NET: Deleting a Registry Value failed
Author: Message:
ryxdp
Senior Member
****


Posts: 804
Reputation: 16
29 / Male / Flag
Joined: Jun 2006
O.P. Visual Basic .NET: Deleting a Registry Value failed
This is what I've got:

vb.net 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 =)

This post was edited on 12-22-2008 at 02:14 AM by ryxdp.
07-12-2008 08:21 AM
Profile PM Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
31 / Male / Flag
Joined: Apr 2004
RE: Visual Basic .NET: Deleting a Registry Value failed
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.
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
07-12-2008 10:09 AM
Profile E-Mail PM Web Find Quote Report
ryxdp
Senior Member
****


Posts: 804
Reputation: 16
29 / Male / Flag
Joined: Jun 2006
O.P. RE: RE: Visual Basic .NET: Deleting a Registry Value failed
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.
07-12-2008 10:16 AM
Profile PM Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15519
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: Visual Basic .NET: Deleting a Registry Value failed
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

This post was edited on 07-12-2008 at 03:13 PM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
07-12-2008 02:55 PM
Profile PM Find Quote Report
ryxdp
Senior Member
****


Posts: 804
Reputation: 16
29 / Male / Flag
Joined: Jun 2006
O.P. RE: Visual Basic .NET: Deleting a Registry Value failed
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:

vb.net 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


This post was edited on 12-22-2008 at 02:13 AM by ryxdp.
07-13-2008 12:26 AM
Profile PM Find Quote Report
pollolibredegrasa
Full Member
***

Avatar
formerly fatfreechicken

Posts: 483
Reputation: 34
35 / Male / Flag
Joined: May 2005
RE: RE: Visual Basic .NET: Deleting a Registry Value failed
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 :)

This post was edited on 07-13-2008 at 01:54 AM by pollolibredegrasa.
;p

[Image: chickennana.gif] Vaccy is my thin twin! [Image: chickennana.gif]
07-13-2008 01:48 AM
Profile PM Find Quote Report
ryxdp
Senior Member
****


Posts: 804
Reputation: 16
29 / Male / Flag
Joined: Jun 2006
O.P. RE: Visual Basic .NET: Deleting a Registry Value failed
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)

vb.net 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


This post was edited on 12-22-2008 at 02:13 AM by ryxdp.
07-13-2008 05:11 AM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On