这篇文章给大家分享的是有关VB.NET如何遍历注册表键的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
VB.NET遍历注册表代码如下:
Imports Microsoft.Win32 Public Class Form1 Dim KeyUsers As RegistryKey ''' <summary> ''' 显示用户列表 ''' summary> ''' <param name="list">显示用户的列表对象param> ''' <remarks>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> ''' 判断用户是否存在 ''' summary> ''' <param name="username">用户名param> ''' <returns>存在返回true,否则返回falsereturns> ''' <remarks>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> ''' 删除用户 ''' summary> ''' <param name="username">被删除的用户名param> ''' <remarks>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> ''' 添加用户 ''' summary> ''' <param name="username">用户名param> ''' <returns>添加成功返回true,否则返回falsereturns> ''' <remarks>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如何遍历注册表键”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。