Developers have always found the windows registry to be a suitable place for storing application specific information and configuration settings. The popularity of the registry can be attributed to the fact that:
(a) registry access is faster than file access .
(b)it is a very secure system-wide data repository.
A folder in the registry is name “key” and the elements in this key are named “values”.
1.How to add a registry Key :
My.Computer.Registry.CurrentUser.CreateSubKey("TestKey")
This line will create a key in HKEY_CURRENT_USER and will be named “TestKey”
2.How to add a registry value for a particular key:
My.Computer.Registry.SetValue("HKEY_CURRENT_USER\TestKey", "TestValue", "This is a value.")
To assign a value, type the complete path, for example “HKEY_CURRENT_USER\TestKey” and then the value name “TestValue” then the value content “This is a value.”.
3.How to read a registry value :
Dim readValue As String readValue = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\TestKey", "TestValue", Nothing) MsgBox("The value is: " & readValue)
We will need to have the Path “HKEY_CURRENT_USER\TestKey” and the name of the value “TestValue”.
4.How to delete a registry key :
My.Computer.Registry.CurrentUser.DeleteSubKey("TestKey")
If we will delete a key then the value of that key will also deleted.The following line will delete a key.
5.Code to check check if a value exist or not:
If My.Computer.Registry.GetValue("HKEY_CURRENT_USER\TestKey", "TestValue", Nothing) Is Nothing Then MsgBox("Value does not exist.") Else MsgBox("Value exist.") End If
To Check this go to Registry by using typing “regedit” in Run.
Note: you can only able to write in HKEY_CURRENT_USER using this code but not in HKEY_LOCAL_MACHINE .