Using Registry Keys with VB.net

64
rate or flag this page

By omarabid


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!

Print   —   Rate it:  up  down  flag this hub

Comments

RSS for comments on this Hub

John  says:
17 months ago

hello,

I was wondering how you would check if a registry key exists.

i have this so far:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If My.Computer.Registry.CurrentUser.GetSubKeyNames("HKEY_CURRENT_USER\Johnn") Is Nothing Then My.Computer.Registry.CurrentUser.CreateSubKey("HKEY_CURRENT_USER\Johnn") End If End Sub

KAli  says:
15 months ago

Video tutorial in you tube about registry Key, worth watching http://www.youtube.com/watch?v=1SFMGsnqDwA

Submit a Comment

Members and Guests

Sign in or sign up and post using a hubpages account.


optional


  • No HTML is allowed in comments, but URLs will be hyperlinked
  • Comments are not for promoting your hubs or other sites

working