Reputation: 5063
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?
Upvotes: 7
Views: 23343
Reputation: 21
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...
Upvotes: 0
Reputation: 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"))
{
}
Upvotes: 0
Reputation: 755457
One way is to use the Registry.OpenSubKey
method
If Microsoft.Win32.Registry.LocalMachine.OpenSubKey("TestKey") Is Nothing Then
' Key doesn't exist
Else
' Key existed
End If
However I would advise that you do not take this path. The OpenSubKey
method returning Nothing
means that the key didn't exist at some point in the past. By the time the method returns another operation in another program may have caused the key to be created.
Instead of checking for the key existence and creating it after the fact, I would go straight to CreateSubKey
.
Upvotes: 8