What object can I use to read and write to windows registry?

borris83

Member
Joined
Apr 30, 2009
Messages
23
Programming Experience
Beginner
In vb.net, What object can I use to read and write to windows registry?

(in vbscript, I use 'regread' in wscript.shell)
 
Thanks..

It looks like using My.Computer.Registry.GetValue, I can get the data of a single value from a registry key, provided I know the value name...

But what if I don't know the value name and loop through all the values in a key and want to read the data for each and every value?

For example, the key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run has all the start up programs... I won't know how many values in the key and what they will be in the user's computer. But I want to read all the vlaues in this key.

Can you help me with it?

I am able to code for a single value :

Dim readValue As String
readValue = My.Computer.Registry.GetValue _
("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "googletalk", Nothing)
MsgBox("The value is " & readValue)


And notice that here I know that the value name is 'googletalk'..
 
My.Computer.Registry.LocalMachine.OpenSubKey(...) returns a RegistryKey instance. This class has several members, including GetValueNames method.
 
Atlast I made it to work the way I wanted.. Thanks for your help..

This imports microsoft.win32


Dim returnValue As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run")
Dim keyname As String = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
Dim values() As String = returnValue.GetValueNames
Dim value
Dim message As String = ""
For Each value In values
Dim readvalue As String
readvalue = Registry.GetValue(keyname, value, Nothing)
message &= value & " : " & readvalue & vbNewLine

Next

MessageBox.Show(message)
 
You already have the key open, no need to call Registry.GetValue which will open the key again and again. You also have to remember to close any key you open when done with it.
VB.NET:
Dim builder As New System.Text.StringBuilder
Dim key As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run")
For Each name As String In key.GetValueNames
    Dim value As String = CStr(key.GetValue(name))
    builder.AppendFormat("{0} : {1}" & vbNewLine, name, value)
Next
key.Close()
MessageBox.Show(builder.ToString)

This imports microsoft.win32
Notice that if you have code like "Dim returnValue As RegistryKey" without that Imports the IDE will underline RegistryKey, now if you hover that a exclamation mark appears with Error Correction Options. In this case the options given is adding the namespace or qualifying the type, when you click either the IDE inserts the correct code for you.
 
I see from the examples hot to read a registry key, but how would I write a particualer key. For example:


[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\AddIns\Clip Redirector]
"Type"=dword:00000003
"Name"="RDPClip"
 
Didn't you see "How to: Create a Registry Key and Set Its Values in Visual Basic" and "How to: Set Values in Registry Keys in Visual Basic" in the Registry object page ?
 
Back
Top