Solved

Access Registry Key Programatically in vb.net

Posted on 2010-09-11
14
878 Views
Last Modified: 2012-05-10
Hi,

I have to access my registry key programatically- Insert a key or update a key using vb.net.

Please provide some code samples

Thanks.
0
Comment
Question by:futurebuilders
  • 7
  • 7
14 Comments
 
LVL 17

Expert Comment

by:Zhaolai
Comment Utility
1. Creating a Subkey

Dim regKey As RegistryKey
regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", True)
regKey.CreateSubKey("MyApp")
regKey.Close()
       

2. Reading and updating values

Dim regKey As RegistryKey
Dim ver As Decimal
regKey = Registry.LocalMachine.OpenSubKey("Software\MyApp", True)
regKey.SetValue("AppName", "MyRegApp")
ver = regKey.GetValue("Version", 0.0)
If ver <  1.1 Then
regKey.SetValue("Version", 1.1)
End If
regKey.Close()

3. Delete a subkey

Dim regKey As RegistryKey
regKey = Registry.LocalMachine.OpenSubKey("Software", True)
regKey.DeleteSubKey("MyApp", True)
regKey.Close()

0
 
LVL 17

Expert Comment

by:Zhaolai
Comment Utility
You also need to add the following line to the top of the class:

Import Microsoft.Win32
0
 

Author Comment

by:futurebuilders
Comment Utility
Hi,

Thanks for your reply. BUt it is bit confusing, like i am unable deside which is key and which is value.here i am specyfying my requirement please provide the sample code o

1) Application checks if registry key exists – “HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\\tcpAckFrequency” the interface GUID is known by there being a key for DHCPipAddress key with a value other than nothing (doesn’t exist) or 0.0.0.0.
2) If not add a Dword(32bit) key called TcpAckFrequency as a hex value “1”
Thanks in advance
0
 
LVL 17

Expert Comment

by:Zhaolai
Comment Utility
Registry Key is like a folder in file system. It can hold multiple registry values.
I believe tcpAckFrequency is not a registry key.

Try this:

        Dim regKey As RegistryKey

        Dim strKey As String = "SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\<INTERFACE GUID>"

        regKey = Registry.LocalMachine.OpenSubKey(strKey, True)

        If regKey IsNot Nothing Then

            If regKey.GetValue("TcpAckFrequency") Is Nothing Then

                regKey.SetValue("TcpAckFrequency", 1, RegistryValueKind.DWord)

            End If

         End If

        regKey.Close()

Open in new window

0
 

Author Comment

by:futurebuilders
Comment Utility
Ok.

So If I want to create a key as below do I need to create one after another like first i hv to create Syste hen I have to create CurrentControlSet then i have to create Services so on....

SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\<INTERFACE GUID>

Please clarify above

thanks in advance
0
 
LVL 17

Accepted Solution

by:
Zhaolai earned 500 total points
Comment Utility
Yes, you have to create one level at a time unfortunately. There is no method to create a key tree, as I know.
But in your case, I believe that you can assume that the key "SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces" always exists on all the machines already.
0
 

Author Comment

by:futurebuilders
Comment Utility
Hi,

I am using windows 7 os.

I am trying to create key with following code
     
        Dim regKey1 As RegistryKey
        regKey1 = Registry.LocalMachine.OpenSubKey("SOFTWARE", True)
        regKey1.CreateSubKey("MyApp")
        regKey1.Close()
 
I think the above code should create key in HKey Local machine but it is creating it in HKey users VS studio ....Virtualstore\machine\software.


May I know why it is creating like that and solution for that

Thnks in advance

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 17

Expert Comment

by:Zhaolai
Comment Utility
That's new to me. I am working on XP machine. I do not have a Win7 to test.
But in debug, what is the regKey1 name? Do you see something like the attached below?

Screenshot.PNG
0
 

Author Comment

by:futurebuilders
Comment Utility
No it is giving object reference not set to an instance

as it is not finging that key
0
 
LVL 17

Expert Comment

by:Zhaolai
Comment Utility
Hmmm... If it errs out, it is not supposed to create any sub key, because it should stop on the error. But you said that it created the key "MyApp" in a different place.
0
 

Author Comment

by:futurebuilders
Comment Utility
Yes when i used above code it is creating key in diff place.But when I am trying to read that key it is gvng that error.

See my req is very simple

I have to check for the kwy whether it exists or not if does not exists i have to create it in the specified location

I think when i use localmachine object, it should create it in HKEY LOCAL path but it is creating in some other path.

Thanks
0
 
LVL 17

Expert Comment

by:Zhaolai
Comment Utility
In the following code,

        Dim regKey1 As RegistryKey
        regKey1 = Registry.LocalMachine.OpenSubKey("SOFTWARE", True)
        regKey1.CreateSubKey("MyApp")
        regKey1.Close()

Place a breakpoint on the 3rd line.
Run it in debug.
When it stops on the breakpoint, mouse over LocalMachine on the 2nd line, what do you see? Anything like the attached below:


Screenshot.PNG
0
 

Author Closing Comment

by:futurebuilders
Comment Utility
thanks
0
 

Author Comment

by:futurebuilders
Comment Utility
thanks
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now