How to Read/Write/Delete Microsoft Windows Registry Keys/Values Using VB .Net?
Microsoft Windows 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
- Playing with Windows Registry - Part 4 Windows registry is one of the most important parts of Microsoft Windows Operating System. It contains information and settings for the Windows itself. Since it contains your Windows setting, you can edit your Windows...
- Playing with Windows Registry: Part 4C Windows registry is one of the most important parts of Microsoft Windows Operating System. It contains information and settings for the Windows itself. Since it contains your Windows setting, you can edit your Windows...
- Playing with Windows Registry: Part 4D Windows registry is one of the most important parts of Microsoft Windows Operating System. It contains information and settings for the Windows itself. Since it contains your Windows setting, you can edit your Windows...
- Windows Desktop Security Tips Windows is not famous for having a lot of effective default security settings to choose from, but as an administrator, you can quickly control just about every aspect of the user's working environment, network or local.
- How to Easily Clean Your Windows Registry This tutorial will show you how to quickly and easily clean the registry within the Windows operating system.
- Playing with Windows Registry - Part 1
- Playing with Windows Registry - Part 3
- Face Your Fears: The Windows XP Registry
- Using DOS Commands with Windows XP Home, XP Professional and Vista
- How to Protect Microsoft Windows from Viruses and Know More About Microsoft in Gen...
- Anatomy of the Windows Registry
- Playing with Windows Registry - Part 4B
|
|
- Basic functions to read, write, delete Microsoft Windows registry content/keys
- Visual Basic .net
2 Comments
Post a Commentthanks for sharing! nice article. Very helpful.
Great work..