1 2 3

How to Read/Write/Delete Microsoft Windows Registry Keys/Values Using VB .Net?

Microsoft Windows Registry

Paresh
Many applications stores useful information like some keys, history, some recently used files information and many more into the registry. If you are also planning to build your own software then you should know about how to read and write into the registry.

One day I was also building my Software and I tried searching the same thing on the net but very few things I have got so after doing some reading, I want to share my knowledge with you all. I tried implementing the same thing (Writing into the Registry and Reading it again) using VB .net

Very first thing that you need to have before we start writing or calling Registry related APIs or Functions in VB .net. Import Win32 Namespace.

Imports Microsoft.Win32

Now you can start using all Registry related functions and classes in your project.

You need to have one variable of type RegistryKey, this key can be used to read / write registry.

' This is how you have to define variable

Dim registryKey As RegistryKey

Get the object of the Registry Machine using below Code of the statement.

registryKey = Registry.LocalMachine

Now to read Registry Key, we have to specify the Registry Path to know whether that Registry Subkey is available or now we have to use below line of code.

Dim registrySubKey As RegistryKey

' Here we are creating SubKey and finding whether the path is available or not creating SubKey with Registry Path

registrySubKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\MICROSOFT\KEY", True)

' Checking whether the above specified Path is available or not in your machine and If that specified registry path is not available then below line is creating it.

If registrySubKey Is Nothing Then
> > >registrySubKey = registryKey.CreateSubKey("SOFTWARE\MICROSOFT\KEY")

End If

After creating Registry Sub Key, if you want to Read some particular Key and its values then you need to write below lines of code.

Dim Data = registrySubKey.GetValue ( "BHATE" )

In above line, "BHATE" is a key and we want to read Value associated with it, so it acts like a "KEY" - "HASH" pair. "BHATE" is acts as a KEY and "BHARTI AIRTEL" as a Value.

So in Data variable you will get "BHARTI AIRTEL" String.

In a similar way we can Set values to any KEYS. Have a look at the below line of code.

registrySubKey.SetValue ( "FCSSOF", "FCS SOFTWARE" )

To Find out number of Keys available in one subKeys again we have to open that Registry Path similar to the one we did above to create/to check whether registry path is available or not.

registrySubKey = registryKey.CreateSubKey("SOFTWARE\MICROSOFT\KEY")

Then below lines of code can be use to know number of KEYS available in the specified Path.

' read all values under the SubKey

Dim regSubKeyValue() As String = registrySubKey.GetValueNames

' This line will tell you the number of KEYS available in that SubKey (specified above registry path)

Dim KeysTotal = regSubKeyValue.Length

To Delete any Key-Value Pair from the Registry, you have to use...

registrySubKey.DeleteValue (regSubKeyValue())

At the end Don't forgot to Close your Registry Objects

registrySubKey.Close()

registryKey.Close()

I hope this will help you to manipulate Registry, these are the basic functions you can use for Windows Registry.
Please refer code snap shot for the each above mentioned functionality. Thanks!

Published by Paresh

I love to do Painting, Sketching, Photography and many more things. I started writing on things which interests me. I am not an expert in writing but I am trying to be a good writer :) Most of the time yo...   View profile

2 Comments

Post a Comment
  • sunil 12/4/2009

    thanks for sharing! nice article. Very helpful.

  • samaira 12/3/2009

    Great work..

Displaying Comments

To comment, please sign in to your Yahoo! account, or sign up for a new account.