Dim sb As New System.Text.StringBuilder ''返回遍历结果
Dim sb2 As New System.Text.StringBuilder ''返回读取出错的注册表键
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 ''返回当前用户键
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
''遍历注册表键树
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
''遍历某键下的项
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