Solved

Access Registry Key Programatically in vb.net

Posted on 2010-09-11
14
887 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
14 Comments
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33654578
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
ID: 33654580
You also need to add the following line to the top of the class:

Import Microsoft.Win32
0
 

Author Comment

by:futurebuilders
ID: 33665447
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:Zhaolai
ID: 33665821
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
ID: 33665947
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
ID: 33666170
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
ID: 33666289
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
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33666432
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
ID: 33666690
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
ID: 33666739
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
ID: 33666839
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
ID: 33666970
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
ID: 34478722
thanks
0
 

Author Comment

by:futurebuilders
ID: 34478724
thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

705 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