Solved

Access Registry Key Programatically in vb.net

Posted on 2010-09-11
14
879 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

12 Experts available now in Live!

Get 1:1 Help Now