Vb. NET to access the registry

Source: Internet
Author: User
Access | registry

Access to the registry in vb.net becomes very simple. We can use Microsoft. Win32 the registry class and RegistryKey class under the namespace. Alternatively, My.Computer.Registry can return an instance of a Microsoft.Win32.Registry class.
Here are a few small examples to illustrate how vb.net accesses the registry.

1, return or create a registry key

Dim Key1 as Microsoft.Win32.RegistryKey
Key1 = My.Computer.Registry.CurrentUser ' Returns the current user key
Dim Key2 as Microsoft.Win32.RegistryKey
Key2 = Key1.opensubkey ("Northsnow") ' Returns the Northsnow key under the current user key
If Key2 is nothing Then
Key2 = Key1.createsubkey ("Northsnow") ' Create it if the key does not exist
End If

2, delete registry keys


Dim Key1 as Microsoft.Win32.RegistryKey
        Key1 = my.computer.registry.currentuser   ' Returns the current user key
        Dim Key2 as Microsoft.Win32.RegistryKey
     
        Key2 = Key1.opensubkey ("Northsnow")        ' returns the Northsnow key under the current user key
         If not Key2 are nothing Then
             Key1.deletesubkey ("Northsnow")   ' create it if the key does not exist
        End If
 3, creating or reading a registry key

Dim Key1 as Microsoft.Win32.RegistryKey
Key1 = My.Computer.Registry.CurrentUser ' Returns the current user key
Dim Key2 as Microsoft.Win32.RegistryKey
Key2 = Key1.opensubkey ("Northsnow", True) returns the Northsnow key under the current user key, and if you want to create an item, you must specify that the second argument is True
If Key2 is nothing Then
Key2 = Key1.createsubkey ("Northsnow") ' Create it if the key does not exist
End If

' Creates an item, if it is not present, and if it exists, overwrites the
Key2.setvalue ("name", "Northern Snow")
Key2.setvalue ("Sex", True)
Key2.setvalue ("Age", 30)

' Return item value
Dim SB as New System.Text.StringBuilder
Sb. Appendline (Key2.getvalue ("name"))
Sb. Appendline (Key2.getvalue ("Sex"))
Sb. Appendline (Key2.getvalue ("Age"))
MsgBox (sb.) ToString)

' Check if an item exists
If (Key2.getvalue ("name")) is nothing Then
MsgBox ("no")
Else
MsgBox ("yes")
End If

If (Key2.getvalue ("name2")) is nothing Then
MsgBox ("no")
Else
MsgBox ("yes")
End If

' Output
' The snow in the northern
' True
' 30
' Yes
' No
4, traversing the registry

This is also very simple, put a button on the form and two text boxes, add the following code

Dim SB as New System.Text.StringBuilder ' returns traversal results
Dim SB2 as New System.Text.StringBuilder ' returns registry key for read error
Private Sub Button3_Click () Sub Button3_Click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles button3.c Lick
Dim Key1 as Microsoft.Win32.RegistryKey
Key1 = My.Computer.Registry.CurrentUser ' Returns the current user key
If not Key1 are nothing Then
Sb. Appendline (Key1.name)
ReadValue (Key1)
Readreg (Key1)
End If
Me.TextBox1.Text = sb. Tostring
Me.TextBox2.Text = SB2. Tostring
End Sub
' Traverse registry key tree
Private Sub Readreg () Sub Readreg (ByVal R as Microsoft.Win32.RegistryKey)
If r.subkeycount > 0 Then
Dim KeyName () as String
Dim Keytemp as Microsoft.Win32.RegistryKey
KeyName = R.getsubkeynames
Dim I as Integer
For i = 0 to Keyname.getlength (0)-1
Try
Sb. Appendline (KeyName (i))
Keytemp = R.opensubkey (KeyName (i), True)
ReadValue (keytemp)
Readreg (keytemp)
Catch ex as Exception
SB2. Appendline (KeyName (i))
End Try
Next
End If
End Sub
' Iterate over the item under a key
Private Sub ReadValue () Sub ReadValue (ByVal R as Microsoft.Win32.RegistryKey)
If r.valuecount > 0 Then
Dim ValueName () as String
Dim I as Integer
VALUENAME = R.getvaluenames
For i = 0 to Valuename.getlength (0)-1
Sb. Appendline ("#")
Sb. Append (R.name)
Sb. Append ("----")
Sb. Append (R.getvalue (ValueName (i)). ToString)
Next
End If
End Sub




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.