Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

Basic Registry manipulation in VB.NET

Author: Aadil

This article describes how you can use the windows registry in your .net programs using the Microsoft.Win32 namespace

The windows registry is a place for storing application data provided by the Windows operating system. Microsoft Windows uses it to store information relating to the operating system and many more.


You can also use the registry to store your application specific information in the registry. In Microsoft .NET Framework the namespace Microsoft.Win32 provides classes for interacting with the registry. The Registry class provides some static fields which refer to the base registry keys such as Registry.LocalMachine refer to the HKEY_LOCAL_MACHINE root key.


You can get a reference to any of the root registry keys (also known as hives) by referring to these static fields. These return an object of type RegistryKey.

Dim rootLocalMachine as RegistryKey = Registry.LocalMachine

The RegistryKey class have methods and properties enabling you to read and write new entries under the opened key. For example to open a subkey named Software, under the rootLocalMachine opened above, you can use the OpenSubKey() method

Dim registryKeySoftware as RegistryKey = rootLocalMachine.OpenSubKey(“Software”, True)

The second parameter in the OpenSubKey() method is a Boolean value specifying weather you want to open this key as writable or not. I mentioned True so this means I want to write something under this key.


Now when you have a RegistryKey object you can read and write values using the GetValue() and SetValue() functions respectively. The SetValue() function takes a name and value pair, the name as String and value as any Object type. The GetValue() function returns a value as Object type for the requested name. You need to cast the value back to the desired object when using the GetValue() function.

registryKeySoftware.SetValue(“yourName”, “AMK”)

After doing any changes you can call the Flush() methor of RegistryKey object to write the changes made in the registry. Then you can call Close() to close the key.



 The procedure for reading the key is also the same. Just call GetValue() of the RegistryKey object and it will return the value as Object type. Then you can cast it back to the desired type using CType() method.

Dim value as String = CType(registryKeySoftware.GetValue(“yourName”), String)

You should call Close() for all opened registry keys for properly releasing the resources.


registryKeySoftware = Nothing

Article Comments
Need windows registry guied in

Posted on 2/7/2007 11:10:22 PM by piyuhsh patel

Add Article Comment:
Name :
Email Address :
Comments :
<< Writing an XML file using WriteXML method of DataSet and reading XML file with the help of ReadXML method of DataSet.

Disclaimer - Privacy
© 2002-2017