Using Registry keys with VB.net

Now we pass to an important part. How to use the registry keys (add, delete and modify) with VB.net
First let's start: Create a new project, and add a button where you'll put the code.
Open the registry editor to check that the changes are applied correctly.
Now as you see there's keys (that are folder) an contain value.
Let's start by making a key.
This will make a key in the Current user root folder.
My.Computer.Registry.CurrentUser.CreateSubKey("TestKey")
Now expand the The Current User root folder and you'll find a new key (folder) made!
Lety set a value on it.
My.Computer.Registry.SetValue("HKEY_CURRENT_USER\TestKey", _"TestValue", "This is a test value.")
As you'll see a new "REG_SZ" value was added.
Very Important : If you want to presice the registry key value type just type ',' and Visual Studio editor will list you the value types.
Now let's read what we wrote!

Dim readValue As String
readValue = My.Computer.Registry.GetValue _
("HKEY_CURRENT_USER\TestKey", "TestValue", Nothing)
MsgBox("The value is " & readValue)


Let's finish by deleting it.
My.Computer.Registry.CurrentUser.DeleteSubKey("TestKey")
I think now you know a lot about Registry keys with vb.net but just now we didn't affrod the most important thing : Modify registry keys made by the System (windows)!
This can be difficult but we'll do it, now watch out this code.

autoshell = Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows NT\CurrentVersion\Winlogon", True)
'' Set the value to 0
autoshell.SetValue("autorestartshell", 0)
autoshell.Close()


The following code change the "Autorestartshell" to 0. be carefull to make the following steps. Make a new object and set i to a Registry.Rootfolder.opensubkey("the sub key",True) If True is false or not set the change will fail!
Now set the value with Name.Setvalue("valuename", depend on the value type)
Finally don't forget to close the key!
Have questions ? Post a comment and I'll reply with a comment!


0 comments:

Post a Comment