|
-
Jul 17th, 2026, 11:59 AM
#1
Thread Starter
Junior Member
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.
-
Re: Registry.CurrentUser.DeleteSubkey issues VS2026 VB10
You code is doing three things:
- It accesses the current Windows user's registry area via HKEY_CURRENT_USER
- It optionally creates but always opens a registry key named MyTestKey
- 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
-
Thread Starter
Junior Member
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.
-
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|