Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to complete VB.NET to read and write registry

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to read and write the registry of VB.NET. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

When programmers choose which programming language to use to help them achieve their own program development, the first factor to consider is a practical, flexible and efficient language. And VB.NET is such a more suitable programming language. For example, it is very easy for VB.NET to read and write the registry. We can use the registry class and registryKey class under the microsoft.Win32 namespace. Alternatively, My.Computer.Registry can return an instance of the Microsoft.Win32.Registry class.

Here are a few small examples to illustrate how VB.NET reads and writes the registry.

VB.NET reads and writes registry 1, returns or creates 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")

'return the northkeeper key under the current user key

If Key2 Is Nothing Then

Key2 = Key1.CreateSubKey ("northsnow")

'create a key if it doesn't exist

End If

VB.NET reads and writes registry 2, deletes 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")

'return the northkeeper key under the current user key

If Not Key2 Is Nothing Then

Key1.DeleteSubKey ("northsnow")

'create a key if it doesn't exist

End If

VB.NET reads and writes registry 3, creating or reading registry keys

Dim Key1 As Microsoft.Win32.RegistryKey

Key1 = My.Computer.Registry.CurrentUser

'return to the current user key

Dim Key2 As Microsoft.Win32.RegistryKey

Key2 = Key1.OpenSubKey ("northsnow", True)

'return the northkeeper key under the current user key. If you want to create an item,

The second parameter must be specified as true

If Key2 Is Nothing Then

Key2 = Key1.CreateSubKey ("northsnow")

'create a key if it doesn't exist

End If

'create an item, create it if it does not exist, or overwrite it if it exists

Key2.SetValue ("name", "Snow in the North")

Key2.SetValue ("sex", True)

Key2.SetValue ("age", 30)

'return the 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

The snow in the north of Saibei

'True

'30

'yes

'no

VB.NET reads and writes registry 4 and traverses the registry

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

'return the traversal result

Dim sb2 As New System.Text.StringBuilder

'return the registry key with the 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 to 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

'iterate through 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

On how to complete the VB.NET read and write registry to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report