這篇文章給大家分享的是有關(guān)VB.NET如何遍歷注冊(cè)表鍵的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
專注于為中小企業(yè)提供成都做網(wǎng)站、網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)潁東免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
VB.NET遍歷注冊(cè)表代碼如下:
Imports Microsoft.Win32 Public Class Form1 Dim KeyUsers As RegistryKey '''''' 顯示用戶列表 ''' summary> ''' 顯示用戶的列表對(duì)象param> ''' remarks> Private Sub ListAllUsers(ByVal list As ListBox) '清除列表中的內(nèi)容 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 '刪除該用戶對(duì)應(yīng)的鍵 KeyUsers.DeleteSubKey(username) ListAllUsers(ListBox1) End If End Sub '更多vb.net源代碼和實(shí)例,請(qǐng)關(guān)注樂(lè)博網(wǎng)lob.cn ''' ''' 添加用戶 ''' summary> ''' 用戶名param> ''' 添加成功返回true,否則返回falsereturns> ''' remarks> Private Function AddUser(ByVal username As String) As Boolean Try '判斷用戶是否存在 If (IsUserExists(username)) Then MsgBox("用戶已經(jīng)存在,請(qǐng)輸入其它用戶!") Return False End If '創(chuàng)建用戶鍵 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設(shè)置為True確??蓪?xiě) key = My.Computer.Registry.CurrentUser.OpenSubKey("Software", True) '在HKEY_CURRENT_USER\Softwar下面創(chuàng)建子鍵并返回創(chuàng)建的子鍵對(duì)象 mykey = key.CreateSubKey("MyKey", RegistryKeyPermissionCheck.ReadWriteSubTree) '創(chuàng)建值并且設(shè)置值的內(nèi)容 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
感謝各位的閱讀!關(guān)于“VB.NET如何遍歷注冊(cè)表鍵”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!