none
How to use (add, read, change, delete) registry keys with vb.net RRS feed

  • General discussion

  • Find this tutorial on Dot Net Source
    Reason of this project:

    This is the next part of the previous tutorial. We'll learn here how to add, change, read, delete registry keys and values using vb.net.
    I included a sample that contains all those functionality.
    Project details:
    1- How to add a registry key/value
    2- How to read a registry value
    3- How to delete a key or a value
    4- Changing a value or a key
    5- Hints to use registry with VB.net
    6- The registry reader (VB.net)

    1- How to add a registry key/value

    One thing that I think I forget to notice. A folder in the registry is name "key" and the elements in this key are named "values".
    There's a description of each value type in the 5th tutorial
    Now we'll see how to add a key or a value.
    Probably you have thought where we'll put it and whether it's a key or a value.
    So we have 2 things to notice.
    Visual Basic will automatically show you the hives (they are stored in).
    You'll have only to choose the needed one.
    To do that paste the following line.
    My.Computer.Registry.CurrentUser.CreateSubKey("TestKey")

    This line will create a key in the HKEY_CURRENT_USER hive and will be named "testkey"
    Now let's move on to see how to set a value.
    For the value we'll need three things: Value path, Value name and value value. We can also precise the value type if not Visual Basic will try to assign the value type depending on the object type.
    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\TestKey", "TestValue", "This is a test value.")

    Tip: Type "," and Visual Basic will show a list of value types.

    The sample:
    The sample contain two part, one for creating keys and the other to create values.
    To create a key just put the named of the key and it'll be created in the Current_User folder.
    To assign a value, type the complete path, for example "HKEY_CURRENT_USER\mykey" and then the value name then the value content and click add to create it.

    2- How to read a registry value

    The next thing is to read what we wrote!
    This is so simple, just put the following line.
    You'll need to have the Path and the name of the value.
    Dim readValue As String
    readValue = My.Computer.Registry.GetValue _
    ("HKEY_CURRENT_USER\TestKey", "TestValue", Nothing)
    MsgBox("The value is " & readValue)

    If you wish to do more complex things, like getting the keys in a hive and getting the values of keys...
    Then you should see the registry viewer sample. (Download the source code to get it)

    3- How to delete a registry key or value

    We finish by deleting what we added.
    To do this it's easy!
    The following line will delete a key
    My.Computer.Registry.CurrentUser.DeleteSubKey("TestKey")

    To delete a value
    my.Computer .Registry .CurrentUser.DeleteValue("Test value")


    4- Changing a value or a key

    Changing a value can be somewhat difficult.
    Dim autoshell = My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows NT\CurrentVersion\Winlogon", True)
    '' Set the value to 0
    autoshell.SetValue("autorestartshell", 0)
    autoshell.Close()
    After opening a subkey (don't forget true! as it give you permission to delete), we can use the autoshell in order to change any value in the following subkey.

    5- Hints to use registry with VB.net

    We can count the values in a hive
    My.Computer.Registry.CurrentUser.ValueCount.ToString()

    But also the keys
    My.Computer.Registry.CurrentUser.SubKeyCount.ToString()

    And check if a value exist
    If My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\MyKey", _
    "TestValue", Nothing) Is Nothing Then
    MsgBox("Value does not exist.")
    Else
    MsgBox("Value exist.")
    End If

    6- The registry reader (VB.net)

    I have included an application with the source code. This one will resume all what we have done.
    It's like the registry editor of Windows but still view only and can't edit values.

    Download the source code


    The Zip file contains:
    -The sample source code
    -The registry viewer application
    -The readme.txt file
    -The tutorial.txt file

    Still have question:
    If you have any problem or you found a bug, go to http://thedotnetsource.blogspot.com/ and post a comment in the tutorial where you are having the problem.
    If you have a general question, post it here

    Friday, March 28, 2008 6:28 PM

All replies

  • Omar Abid

     

    Thanks very much for sharing this helpful post! In order to help more community members on accessing the Registry issues, I would like to change this thread type from Question to Comment to make clear that this is helpful thread.

     

    Thanks again for your post!

     

    Tuesday, April 1, 2008 6:14 AM
  • This is a great post but have you considered 64 bit operating systems? If you were to run this on a 64 bit machine the key will get created in HKLM\SOFTWARE\Wow6432\Testkey

     

    Any idea how to write the 64 bit key in the same location as you would find it on the 32 bit machine?

    Wednesday, April 2, 2008 11:38 PM
  • Bruno Yu - MSFT said:

    Omar Abid

     

    Thanks very much for sharing this helpful post! In order to help more community members on accessing the Registry issues, I would like to change this thread type from Question to Comment to make clear that this is helpful thread.

     

    Thanks again for your post!

     


    This can be an ultra late reply but just today i tried to test those forums again that I can access (last times, i can't see the threads)
    What I know that comments don't get indexed (in the old MSDN forums) so that's why i posted it as a question.
    Does this issue still on the new ??

    Full dot net tutorials with code sources : http://thedotnetsource.blogspot.com
    Thursday, July 3, 2008 6:11 PM
  • Hi,

    This is very useful article.

    I have a question regarding this.

    I am trying to insert registry key using Localmachine method. I think this should insert a key in HKEy_localmachine section but instead it is inserting key in local user section.

    How to overcome this.Any idea.Please let me know.

     

    Thanks in advance

     

     

     

     

    Tuesday, September 14, 2010 6:57 AM
  • Hi,

    This is very useful article.

    I have a question regarding this.

    I am trying to insert registry key using Localmachine method. I think this should insert a key in HKEy_localmachine section but instead it is inserting key in local user section.

    How to overcome this.Any idea.Please let me know.

    Thanks in advance


    HI SriResume
    Dim regKey As RegistryKey
    regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", True)
    regKey.CreateSubKey("TestKey")
    regKey.Close() On Windows 7 VS2008 use
    regKey = Registry.LocalMachineRegistry.OpenSubKey("Software", True)
    Try this. It should resolve your issue. Sands.
    Saturday, January 29, 2011 1:52 AM
  • I am trying to pull information from a Registry Hive such as HKEY_CURRENT_USER\Printers I am wanting to pull the folders or keys from the sub folder of Printers. I know I could use the count function to count the number of folders and create a loop that loops through the folders but the problem is I'm not sure how to pull the name of folder from this sub folder. Any ideas?

    Tuesday, March 1, 2011 9:52 PM
  • non of this is really helping me out not enough info on deleting a value or subkey

    i tryed this many ways i get no errors but once i get to testing it i get an error

            My.Computer.Registry.CurrentUser.DeleteValue("Software\Blizzard Entertainment\Diablo II\Default Channel")

    any help would be nice

    Wednesday, June 6, 2012 8:53 PM