十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章给大家分享的是有关VB.NET如何遍历注册表键的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
专注于为中小企业提供成都做网站、网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业颍东免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
VB.NET遍历注册表代码如下:
Imports Microsoft.Win32 Public Class Form1 Dim KeyUsers As RegistryKey '''''' 显示用户列表 ''' summary> ''' 显示用户的列表对象param> ''' 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> ''' 用户名param> ''' 存在返回true,否则返回falsereturns> ''' 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> ''' 被删除的用户名param> ''' 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> ''' 用户名param> ''' 添加成功返回true,否则返回falsereturns> ''' 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如何遍历注册表键”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!