This will guide you, how to use Windows Registry to store user specific non critical data which can be retrieved across applications.
Background
The Registry is split into a number of logical sections better known as hives.
A registry hive is a group of keys, subkeys, and values. While programming for our applications we need to store application specific data. That is usually goes in ‘software’ subtree of HKEY_CURRENT_USER hive or HKEY_CURRENT_USER\Software. The supporting files for this hive goes in ‘%SystemRoot%\Profiles\Username’ folder. While the supporting files for all other hives goes in %SystemRoot%\System32\Config folder.
Using the code
To use Registry in VB.NET you first have to imports ‘Microsoft.Win32’ namespace. This will provide an access to RegistryKey class.
Create Registry Key
'Create a SubKey under Software subtree of HKEY_CURRENT_USER hive.
Dim vRegistryKey As RegistryKey
vRegistryKey = Registry.LocalMachine.OpenSubKey("Software", True)
vRegistryKey.CreateSubKey("NewKey")
vRegistryKey.Close()
'Open Newly created SubKey and insert values using SetValue() method.
Dim vRegistryKey1 As RegistryKey
vRegistryKey1 = Registry.LocalMachine.OpenSubKey("Software\NewKey", True)
vRegistryKey1.SetValue("Key Name", txtKeyName.Text)
vRegistryKey1.SetValue("Key Description", txtKeyDescription.Text)
vRegistryKey1.Close()
Retrieve Existing Registry Key
'Open required SubKey and use GetValue() method to fetch key values.
Dim vRegistryKey As RegistryKey
vRegistryKey = Registry.LocalMachine.OpenSubKey("Software\NewKey", True)
txtKeyName.Text = vRegistryKey.GetValue("Key Name").ToString
txtKeyDescription.Text = vRegistryKey.GetValue("Key Description").ToString
vRegistryKey.Close()
Delete Existing Registry Key
'Open required SubKey and use DeleteSubKey() method to detele subkey.
Dim vRegistryKey As RegistryKey
vRegistryKey = Registry.LocalMachine.OpenSubKey("Software", True)
vRegistryKey.DeleteSubKey("NewKey")
vRegistryKey.Close()
Points to Note
In the provided download sample, when you'll create an Item, please enter a valid Server/IP Address (you can enter 'localhost'). Otherwise an error will be shown.
No comments:
Post a Comment