当前位置:首页 > 编程学习 > VB.NET中快速访问注册表技巧

VB.NET中快速访问注册表技巧

编程学习2007-04-2958020

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,删除注册表键

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,创建或读取注册表项

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,遍历注册表

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

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



扫描二维码推送至手机访问。

版权声明:本文由海阔天空发布,如需转载请注明出处。

本文链接:https://apull.net/html/20070429102.html

分享给朋友:

相关文章

VB/VB.net 只允许运行一个实例

VB/VB.net 只允许运行一个实例

VB/VB.net  只允许运行一个实例       by:  Apull  VB中的实现方法VB如何防止程序被重复加载,也就是只允许运行一个实例,一般有2种方法。一,直接用VB提供的APP对象的PrevInstance方法。PrevInstance的返回值为True则说明已经有一个实力在运行。If App.PrevInstance Then Msgbox "已经有一个实例在运行" End if这个...

用VB类实现文件对话框

用VB类实现文件对话框

用VB类实现文件对话框'类名:ComDlg.cls '作用:文件打开保存对话框 ' ' ' 'By:Apull '2007-5-21 'http://www.apull.net Option Explicit Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" ( _ pOpenfil...

VB.NET关于加密算法

VB.NET关于加密算法

加密将防止数据被查看或修改,并在原本不安全的信道上提供安全的通信信道,它达到以下目的:  保密性:防止用户的标识或数据被读取。  数据完整性:防止数据被更改。  身份验证:确保数据发自特定的一方。  基本概念  1、散列(HASH)函数  散列(HASH)函数H也称哈希函数或杂凑函数等,是典型的多到一的函数,其输入为一可变长x(可以足够的长),输出一固定长的串h(一般为128位、160位,比输入的串短),该串h被称为输入x的Hash值(或称消息摘要Message  ...

SetTimer(), KillTimer() 使用

SetTimer(), KillTimer() 使用

SetTimer函数的用法  1 )用WM_TIMER来设置定时器  先请看SetTimer这个API函数的原型  UINT_PTR SetTimer(   HWND hWnd, // 窗口句柄   UINT_PTR nIDEvent, // 定时器ID,多个定时器时,可以通过该ID判断是哪个定时器   UINT uElapse, // 时间间隔,单位为毫秒   TIMERPROC lpTimerFunc // 回调函数   );例如  SetTimer...