Windows Registry keys using VB.NET


registry is suitable place for storing application specific information and configuration settings. Traditionally, the registry has been used for storing configuration information like database connection strings, profiles etc. The popularity of the registry can be attributed to the fact that registry access is faster than file access and also because it is a very secure system-wide data repository. Moreover, configuration files like INI files had their own limitations.


We'll learn here how to add, change, read, delete registry keys and values using vb.net.
I included a sample that contains all those functionality.

BASICS OF WINDOWS REGISTRY

The registry is organized as a hierarchical structure. It has basically five predefined keys under which all data is added or accessed. These keys cannot be renamed or deleted. Given below is a table containing a brief description about them.
Subtree
Definition
HKEY_CURRENT_USER
This contains configuration information of a user who is currently logged on to the system.That is, user profile data is stored here
HKEY_USERS
Contains all user profiles on the computer. HKEY_CURRENT_USER is actually an alias for a key in the HKEY_USERS sub tree.
HKEY_LOCAL_MACHINE
Contains configuration information particular to the computer, irrespective of which user is logged on.
HKEY_CLASSES_ROOT
Contains data that associates file types with programs, and configuration data for COM objects.
HKEY_CURRENT_CONFIG
Contains information about the hardware profile used by the local computer at system startup.
Each key has many subkeys and may have a value. Given below is a snapshot of the registry as seen through the registry editor (Regedit.exe), which comes along with windows.

Fig 1: Registry Structure
In the snapshot shown above, each node under My Computer is a key. For example , HKEY_CURRENT_CONFIG is a key which has two subkeys: Software and System. Fonts is a subkey under software and has values. Each Valuecontains a name and its associated data. Each value needs to be associated with a particular data type. Given below is a table containing the important data types
Data types
Used for
REG_SZ
A fixed-length text string. Boolean (True or False) values and other short text values usually have this data type.
REG_EXPAND_SZ
A variable-length text string that can include variables that are resolved when an application or service uses the data.
REG_DWORD
Data represented by a number that is 4 bytes (32 bits) long.
REG_MULTI_SZ
Multiple text strings formatted as an array of null-terminated strings, and terminated by two null characters.
Referring back to figure 1, there is a value called LogPixels which has data of type REG_DWORD and value 96.

WORKING WITH MICROSOFT.WIN32 NAMESPACE

The operations on the registry in .NET can be done using two classes of the Microsoft.Win32 NamespaceRegistry class and the RegistryKey class.The Registry class provides base registry keys as shared public (read-only) methods: 
ClassesRoot
This field reads the Windows registry base key HKEY_CLASSES_ROOT.
CurrentConfig
Reads the Windows registry base key HKEY_CURRENT_CONFIG.
CurrentUser
Reads the Windows registry base key HKEY_CURRENT_USER
LocalMachine
This field reads the Windows registry base key HKEY_LOCAL_MACHINE.
Users
This field reads the Windows registry base key HKEY_USERS.
Each of the public methods shown above provides an object of the RegistryKey class whose methods can be used to access subkeys under the corresponding keys. The important members of the RegistryKey class are enlisted below

Public Properties

Name
Retrieves the name of the key.
SubKeyCount
Retrieves the count of subkeys at the base level, for the current key.
ValueCount
Retrieves the count of values in the key.

Public Methods

Close
Closes the key and flushes it to disk if the contents have been modified.
CreateSubKey
Creates a new subkey or opens an existing subkey.
DeleteSubKey
Deletes the specified subkey.
DeleteSubKeyTree
Deletes a subkey and any child subkeys recursively.
DeleteValue
Deletes the specified value from this key.
Flush
Writes all the attributes of the specified open registry key into the registry.
GetSubKeyNames
Retrieves an array of strings that contains all the subkey names.
GetValue
Retrieves the specified value.
GetValueNames
Retrieves an array of strings that contains all the value names associated with this key.
OpenSubKey
Retrieves a specified subkey, with the write access as specified.
SetValue
Sets the specified value.

VB.NET AND REGISTRY

Project details:
1- How to add a registry key/value
2- How to read a registry value
3- How to delete a key or a value
4- Changing a value or a key
5- Hints to use registry with VB.net
6- The registry reader (VB.net)

1- How to add a registry key/value

One thing that I think I forget to notice. A folder in the registry is name "key" and the elements in this key are named "values".
There's a description of each value type in the 5th tutorial
Now we'll see how to add a key or a value.
Probably you have thought where we'll put it and whether it's a key or a value.
So we have 2 things to notice.
Visual Basic will automatically show you the hives (they are stored in).
You'll have only to choose the needed one.
To do that paste the following line.
My.Computer.Registry.CurrentUser.CreateSubKey("TestKey")

This line will create a key in the HKEY_CURRENT_USER hive and will be named "testkey"
Now let's move on to see how to set a value.
For the value we'll need three things: Value path, Value name and value value. We can also precise the value type if not Visual Basic will try to assign the value type depending on the object type.
My.Computer.Registry.SetValue("HKEY_CURRENT_USER\TestKey", "TestValue", "This is a test value.")

Tip: Type "," and Visual Basic will show a list of value types.

The sample:
The sample contain two part, one for creating keys and the other to create values.
To create a key just put the named of the key and it'll be created in the Current_User folder.
To assign a value, type the complete path, for example "HKEY_CURRENT_USER\mykey" and then the value name then the value content and click add to create it.

2- How to read a registry value

The next thing is to read what we wrote!
This is so simple, just put the following line.
You'll need to have the Path and the name of the value.
Dim readValue As String
readValue = My.Computer.Registry.GetValue _
("HKEY_CURRENT_USER\TestKey", "TestValue", Nothing)
MsgBox("The value is " & readValue)

If you wish to do more complex things, like getting the keys in a hive and getting the values of keys...
Then you should see the registry viewer sample. (Download the source code to get it)

3- How to delete a registry key or value

We finish by deleting what we added.
To do this it's easy!
The following line will delete a key
My.Computer.Registry.CurrentUser.DeleteSubKey("TestKey")

To delete a value
my.Computer .Registry .CurrentUser.DeleteValue("Test value")


4- Changing a value or a key

Changing a value can be somewhat difficult.
Dim autoshell = My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows NT\CurrentVersion\Winlogon", True)
'' Set the value to 0
autoshell.SetValue("autorestartshell", 0)
autoshell.Close()
After opening a subkey (don't forget true! as it give you permission to delete), we can use the autoshell in order to change any value in the following subkey.

5- Hints to use registry with VB.net

We can count the values in a hive
My.Computer.Registry.CurrentUser.ValueCount.ToString()

But also the keys
My.Computer.Registry.CurrentUser.SubKeyCount.ToString()

And check if a value exist
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

6- The registry reader (VB.net)

I have included an application with the source code. This one will resume all what we have done.
It's like the registry editor of Windows but still view only and can't edit values.



if this helps you please follow us on social pages for more like this and more, thank you

Comments