您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。