Solved

Access Registry Key Programatically in vb.net

Posted on 2010-09-11
14
884 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
Industry Leaders: 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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
Winform Module - What is the ASP.Net equiv 2 39
Looping through datagridview and dataset ? 6 33
Asp.Net Session Question 2 36
Set arraylist as datatable 2 10
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 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