大家都遇到过遍历的问题,你拿到了数据就要对它进行遍历才能读出来,在这里我给大家演示一个VB.NET遍历注册表键,并且提供添加,修改,删除键及键的用户。
VB.NET遍历注册表代码如下:
- Imports Microsoft.Win32
- Public Class Form1
- Dim KeyUsers As RegistryKey
- ''' <summary>
- ''' 显示用户列表
- ''' < SPAN>summary>
- ''' <param name="list">显示用户的列表对象< SPAN>param>
- ''' <remarks>< SPAN>remarks>
- Private Sub ListAllUsers(ByVal list As ListBox)
- '清除列表中的内容
- list.Items.Clear()
- '遍历用户键下的子键
- For Each keyname As String In KeyUsers.GetSubKeyNames()
- list.Items.Add(keyname)
- Next
- End Sub
- ''' <summary>
- ''' 判断用户是否存在
- ''' < SPAN>summary>
- ''' <param name="username">用户名< SPAN>param>
- ''' <returns>存在返回true,否则返回false< SPAN>returns>
- ''' <remarks>< SPAN>remarks>
- Private Function IsUserExists(ByVal username As String) As Boolean
- '遍历用户键下的子键
- For Each keyname As String In KeyUsers.GetSubKeyNames()
- '如果用户存在返回true
- If (keyname.ToUpper() = username.ToUpper()) Then
- Return True
- End If
- Next
- Return False
- End Function
- ''' <summary>
- ''' 删除用户
- ''' < SPAN>summary>
- ''' <param name="username">被删除的用户名< SPAN>param>
- ''' <remarks>< SPAN>remarks>
- Private Sub DeleteUser(ByVal username As String)
- If (IsUserExists(username)) Then
- '删除该用户对应的键
- KeyUsers.DeleteSubKey(username)
- ListAllUsers(ListBox1)
- End If
- End Sub
- '更多vb.net源代码和实例,请关注乐博网lob.cn
- ''' <summary>
- ''' 添加用户
- ''' < SPAN>summary>
- ''' <param name="username">用户名< SPAN>param>
- ''' <returns>添加成功返回true,否则返回false< SPAN>returns>
- ''' <remarks>< SPAN>remarks>
- Private Function AddUser(ByVal username As String) As Boolean
- Try
- '判断用户是否存在
- If (IsUserExists(username)) Then
- MsgBox("用户已经存在,请输入其它用户!")
- Return False
- End If
- '创建用户键
- KeyUsers.CreateSubKey(username, _
- RegistryKeyPermissionCheck.ReadWriteSubTree)
- '重新列表
- ListAllUsers(ListBox1)
- Catch ex As Exception
- Return False
- End Try
- End Function
- Private Sub btnCreateKey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateKey.Click
- Dim key As RegistryKey
- Dim mykey As RegistryKey
- '首先需要获得HKEY_CURRENT_USER\Softwar子键,将writable设置为True确保可写
- key = My.Computer.Registry.CurrentUser.OpenSubKey("Software", True)
- '在HKEY_CURRENT_USER\Softwar下面创建子键并返回创建的子键对象
- mykey = key.CreateSubKey("MyKey", RegistryKeyPermissionCheck.ReadWriteSubTree)
- '创建值并且设置值的内容
- mykey.SetValue("UserName", "Rui.Chen", RegistryValueKind.String)
- End Sub
- Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
- If (ListBox1.SelectedIndex >= 0) Then
- btnDelete.Enabled = True
- Else
- btnDelete.Enabled = False
- End If
- End Sub
- Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
- DeleteUser(ListBox1.SelectedItem.ToString())
- End Sub
- Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
- If (TextBox1.Text <> "") Then
- AddUser(TextBox1.Text)
- End If
- End Sub
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- KeyUsers = My.Computer.Registry.CurrentUser.OpenSubKey("Software\MyKey", True)
- ListAllUsers(ListBox1)
- btnDelete.Enabled = False
- End Sub
- End Class
以上是关于VB.NET遍历注册表键的一个实例,大家快试试吧!
【编辑推荐】