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
Solved

Access Registry Key Programatically in vb.net

Posted on 2010-09-11
14
883 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
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net - Windows UserName index and length error 4 34
VB.net and sql server 4 45
Expression Evaluater 3 37
Sql server, import complete table, using vb.net 9 43
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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