VB.NET中访问注册表
  QxmEyLhwiEt2 2023年11月02日 33 0


  vb.net中访问注册表变得非常的简单。我们可以用  microsoft.Win32 名称空间的 下的 registry 类 和  registryKey 类。 另外 My.Computer.Registry 也可以返回一个 Microsoft.Win32.Registry 类的实例。
下面就举几个小例子来说明vb.net访问注册表的方法。
1,返回或创建一个注册表键


Dim 
   Key1  
  As 
   Microsoft.Win32.RegistryKey
        Key1  
  = 
   My.Computer.Registry.CurrentUser    
  ' 
  返回当前用户键 
  
 
           
  Dim 
   Key2  
  As 
   Microsoft.Win32.RegistryKey
        Key2  
  = 
   Key1.OpenSubKey( 
  " 
  northsnow 
  " 
  )        
  ' 
  返回当前用户键下的northsnow键 
  
 
           
  If 
   Key2  
  Is 
    
  Nothing 
    
  Then 
  
            Key2  
  = 
   Key1.CreateSubKey( 
  " 
  northsnow 
  " 
  )   
  ' 
  如果键不存在就创建它 
  
 
           
  End 
    
  If


2,删除注册表键


VB.NET中访问注册表_button

Dim    Key1  
  As 
   Microsoft.Win32.RegistryKey
        Key1    = 
   My.Computer.Registry.CurrentUser    
  ' 
  返回当前用户键 
  
            
  Dim 
   Key2  
  As 
   Microsoft.Win32.RegistryKey
      
        Key2    = 
   Key1.OpenSubKey( 
  " 
  northsnow 
  " 
  )        
  ' 
  返回当前用户键下的northsnow键 
  
            
  If 
    
  Not 
   Key2  
  Is 
    
  Nothing 
    
  Then 
  
            Key1.DeleteSubKey(   " 
  northsnow 
  " 
  )   
  ' 
  如果键不存在就创建它 
  
            
  End 
    
  If


 3,创建或读取注册表项



VB.NET中访问注册表_button

Dim    Key1    As 
   Microsoft.Win32.RegistryKey
        Key1    =    My.Computer.Registry.CurrentUser    
  ' 
  返回当前用户键 
  
              Dim 
   Key2  
  As 
   Microsoft.Win32.RegistryKey
        Key2    =    Key1.OpenSubKey( 
  " 
  northsnow 
  " 
  ,  
  True 
  )        
  ' 
  返回当前用户键下的northsnow键,如果想创建项,必须指定第二个参数为true 
  
              If 
   Key2  
  Is 
    
  Nothing 
    
  Then 
  
            Key2    =    Key1.CreateSubKey( 
  " 
  northsnow 
  " 
  )   
  ' 
  如果键不存在就创建它 
  
              End 
    
  If 
  

           '   创建项,如果不存在就创建,如果存在则覆盖 
  
           Key2.SetValue(   " 
  name 
  " 
  ,  
  " 
  塞北的雪 
  " 
  )
        Key2.SetValue(   "   sex 
  " 
  ,  
  True 
  )
        Key2.SetValue(   "   age 
  " 
  ,  
  30 
  )

           '   返回项值 
  
              Dim 
   sb  
  As 
    
  New 
   System.Text.StringBuilder
        sb.AppendLine(Key2.GetValue(   "   name 
  " 
  ))
        sb.AppendLine(Key2.GetValue(   "   sex 
  " 
  ))
        sb.AppendLine(Key2.GetValue(   "   age 
  " 
  ))
           MsgBox   (sb.ToString)

           '   查验某个项是否存在 
  
              If 
   (Key2.GetValue( 
  " 
  name 
  " 
  ))  
  Is 
    
  Nothing 
    
  Then 
  
               MsgBox   ( 
  " 
  no 
  " 
  )
           Else   
               MsgBox   ( 
  " 
  yes 
  " 
  )
           End     
  If 
  

           If    (Key2.GetValue( 
  " 
  name2 
  " 
  ))  
  Is 
    
  Nothing 
    
  Then 
  
               MsgBox   ( 
  " 
  no 
  " 
  )
           Else   
               MsgBox   ( 
  " 
  yes 
  " 
  )
           End     
  If 
  

   '   输出  
  
'     塞北的雪   
'   True   
'   30   
'   yes   
'   no



4,遍历注册表
这个也非常简单,在窗体上放一个按钮和两个文本框,添加如下的代码




VB.NET中访问注册表_button

Dim    sb    As 
    
  New 
   System.Text.StringBuilder   
  ' 
  返回遍历结果 
  
          Dim    sb2  
  As 
    
  New 
   System.Text.StringBuilder   
  ' 
  返回读取出错的注册表键 
  
          Private     
  Sub Button3_Click() 
  Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim Key1 As Microsoft.Win32.RegistryKey
        Key1 = My.Computer.Registry.CurrentUser   '返回当前用户键
        If Not Key1 Is Nothing Then
            sb.AppendLine(Key1.Name)
            readValue(Key1)
            readReg(Key1)
        End If
        Me.TextBox1.Text = sb.ToString
        Me.TextBox2.Text = sb2.ToString
    End Sub   
       '   遍历注册表键树   
          Private     
  Sub readReg() 
  Sub readReg(ByVal r As Microsoft.Win32.RegistryKey)
        If r.SubKeyCount > 0 Then
            Dim keyName() As String
            Dim keyTemp As Microsoft.Win32.RegistryKey
            keyName = r.GetSubKeyNames
            Dim i As Integer
            For i = 0 To keyName.GetLength(0) - 1
                Try
                    sb.AppendLine(keyName(i))
                    keyTemp = r.OpenSubKey(keyName(i), True)
                    readValue(keyTemp)
                    readReg(keyTemp)
                Catch ex As Exception
                    sb2.AppendLine(keyName(i))
                End Try
            Next
        End If
    End Sub   
       '   遍历某键下的项   
          Private     
  Sub readValue() 
  Sub readValue(ByVal r As Microsoft.Win32.RegistryKey)
        If r.ValueCount > 0 Then
            Dim valueName() As String
            Dim i As Integer
            valueName = r.GetValueNames
            For i = 0 To valueName.GetLength(0) - 1
                sb.AppendLine("####")
                sb.Append(r.Name)
                sb.Append("----")
                sb.Append(r.GetValue(valueName(i)).ToString)
            Next
        End If
    End Sub


 



【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

上一篇: 这个周末 下一篇: 用StreamReader读文件
  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
QxmEyLhwiEt2
最新推荐 更多

2024-05-17