Results 1 to 4 of 4

Thread: [RESOLVED] Registry.CurrentUser.DeleteSubkey issues VS2026 VB10

  1. #1

    Thread Starter
    Junior Member scot-65's Avatar
    Join Date
    Mar 2013
    Posts
    18

    Resolved [RESOLVED] Registry.CurrentUser.DeleteSubkey issues VS2026 VB10

    Given the following:
    Code:
    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\MyTestKey", "MyTestKeyValue", "This is a test value.")

    I wish to do one of the following:
    a. Delete "MyTestKeyValue" or
    b. Set "This is a test value" to Nothing.

    This is in part to reset settings such as Background Color, Width, Height, Position-x, Position-y (all are REG_DWORD).
    DeleteSubKey is not working. Error says key does not exist.
    SetValue to Nothing also throws errors.

    Thanks in advance.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,407

    Re: Registry.CurrentUser.DeleteSubkey issues VS2026 VB10

    You code is doing three things:
    1. It accesses the current Windows user's registry area via HKEY_CURRENT_USER
    2. It optionally creates but always opens a registry key named MyTestKey
    3. Inside the key, it optionally creates, but always updates a registry value named MyTestKeyValue


    The reason why DeleteSubKey (presumably RegistryKey.DeleteSubKey [doc]) fails is because it searches for a child key but what you have created is a child value.

    What you could do is use the RegistryKey.DeleteValue method [doc] for option A:
    Code:
    Using myTestKey As RegistryKey = Registry.CurrentUser.OpenSubKey("MyTestKey", True)
        If (myTestKey IsNot Nothing) Then
            myTestKey.DeleteValue("MyTestValue", True)
        End If
    End Using
    What this does is get the registry key named MyTestKey and then deletes the MyTestValue value from the registry key. Keep in mind that the throwOnMissingValue argument is true in my example which means that it will throw an exception if you attempt to delete the value but it does not exist.

    For option B, simply use String.Empty instead of Nothing:
    Code:
    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\MyTestKey", "MyTestKeyValue", String.Empty)
    Then in your code when you get the registry value, check if the value is not null or whitespace:
    Code:
    Dim myTestKeyValue As String = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\MyTestKey", "MyTestKeyValue", String.Empty).ToString()
    If (Not String.IsNullOrWhiteSpace(myTestKeyValue)) Then
        ' ...
    End If
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Junior Member scot-65's Avatar
    Join Date
    Mar 2013
    Posts
    18

    Re: Registry.CurrentUser.DeleteSubkey issues VS2026 VB10

    @dday9,

    DeleteValue seems to be what I was looking for.

    Code:
             Using RKey As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\MySorter", True)
                RKey?.DeleteValue("BGColor", False)
                RKey?.DeleteValue("Height", False)
                RKey?.DeleteValue("Width", False)
                RKey?.DeleteValue("Position-x", False)
                RKey?.DeleteValue("Position-y", False)
             End Using
    There are a few additional item not shown and a few in which I will keep.

    Thanks.

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,407

    Re: Registry.CurrentUser.DeleteSubkey issues VS2026 VB10

    No problem! Just be sure to mark this thread as resolved via the Thread Tools option up at the top.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width