'Check if a key exists in the Windows Registry with VB.NET

In VB.NET I can create a key in the Windows Registry like this:

My.Computer.Registry.CurrentUser.CreateSubKey("TestKey")

And I can check if a value exists within a key like this:

If My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\MyKey", _
        "TestValue", Nothing) Is Nothing Then
    MsgBox("Value does not exist.")
Else
    MsgBox("Value exist.")
End If

But how can I check if a key with a specific name exists in the Registry?



Solution 1:[1]

I use this code. It’s simple, easy, and it works on HKEY_CURRENT_USER\Software\YourAppSettings.

Code:

string[]  kyes=Registry.CurrentUser.OpenSubKey(@"Software\YourAppSettings").GetValueNames();
if (!kyes.Contains("keytoknowIfExist"))
      {

      }

Solution 2:[2]

Thanks for the information - this helped me a lot! I did notice while I am in the Visual Studio development module however, the settings were being saved under Computer\HKEY_CURRENT_USER\Software\VB and VBA Program Settings\Elementary\Backup - not the same as the finished Installed Software. I am using ie. SaveSetting("Elementary", "Backup", "BackupFile", bdbname)

Since I didn't know exactly where my settings would be saved in the registry once my product was completed, I tried this and it worked perfect for me. I didn't have to know the exact location, which was helpful.

    If GetSetting("Elementary", "Backup", "BackupFile", Nothing) <> Nothing Then
        DeleteSetting("Elementary", "Backup", "BackupFile")
        bdbname = ""
    End If

Anyway, hope it helps someone in the future...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hobwell
Solution 2 Michael Allen