Quick Registry Access in VB. NET

Source: Internet
Author: User

Accessing the Registry in VB.net becomes very simple. We can use the registry class and registrykey class under the Microsoft. Win32 namespace. In addition, my. Computer. Registry can return an instance of the Microsoft. win32.registry class.

The following is 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 'return the current user key

Dim key2 as Microsoft. win32.registrykey

Key2 = key1.opensubkey ("northsnow ")'

If key2 is nothing then

Key2 = key1.createsubkey ("northsnow") 'creates a key if it does not exist.

End if

 

2. Delete the registry key.

 

Dim key1 as Microsoft. win32.registrykey

Key1 = My. Computer. Registry. currentuser 'return the current user key

Dim key2 as Microsoft. win32.registrykey

Key2 = key1.opensubkey ("northsnow ")'

If not key2 is nothing then

Key1.deletesubkey ("northsnow") 'creates a key if it does not exist.

End if

 

3. Create or read a registry key

4. traverse the Registry

Dim key1 as Microsoft. win32.registrykey

Key1 = My. Computer. Registry. currentuser 'return the current user key

Dim key2 as Microsoft. win32.registrykey

Key2 = key1.opensubkey ("northsnow", true) 'returns the northsnow key under the current user key,

If you want to create an item, you must specify the second parameter as true.

If key2 is nothing then

Key2 = key1.createsubkey ("northsnow") 'creates a key if it does not exist.

End if

'Create item. If it does not exist, it will be created. If it exists, it will overwrite

Key2.setvalue ("name", "Snow in Sebei ")

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 whether 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

'Snow in Sebei

'True'

'30

'Yes

'No

 

 

This is also very simple. Put a button and two text boxes on the form and add the following code:

 

Dim Sb as new system. Text. stringbuilder 'returns the traversal result

Dim sb2 as new system. Text. stringbuilder 'returns the registry key with an error in reading.

Private sub button3_click () sub button3_click (byval sender as system. object,

Byval e as system. eventargs) handles button3.click

Dim key1 as Microsoft. win32.registrykey

Key1 = My. Computer. Registry. currentuser 'return the current user key

If not key1 is 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 the 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

'Traverse the items 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.