VB NET 注册表操作

18 篇文章 0 订阅
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Microsoft.Win32
Imports System.Diagnostics

Class Reg
    ''' <summary>
    ''' 注册表设置值
    ''' </summary>
    ''' <param name="strKey"></param>
    ''' <param name="strName"></param>
    ''' <param name="strValue"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function SetRegistry(ByVal strKey As String, ByVal strName As String, ByVal strValue As String) As Boolean
        Try
            Dim Key_LocalMachine As RegistryKey
            Key_LocalMachine = My.Computer.Registry.LocalMachine 'get the key path of HKEY_LOCAL_MACHINE     
            Dim Key_Created As RegistryKey
            Key_Created = Key_LocalMachine.OpenSubKey(strKey, True) 'set the true for can write
            If Key_Created Is Nothing Then
                Key_Created = Key_LocalMachine.CreateSubKey(strKey) 'careat the key 
            End If
            Key_Created.SetValue(strName, strValue) 'set the values of the key
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
    ''' <summary>
    ''' 获取注册表值
    ''' </summary>
    ''' <param name="strKey"></param>
    ''' <param name="strName"></param>
    ''' <param name="strValue"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function GetValueFromRegistry(ByVal strKey As String, ByVal strName As String, ByRef strValue As String) As Boolean
        Try
            Dim Key_LocalMachine As RegistryKey
            Key_LocalMachine = My.Computer.Registry.LocalMachine 'get the key path of HKEY_LOCAL_MACHINE     
            Dim Key_Get As RegistryKey
            Key_Get = Key_LocalMachine.OpenSubKey(strKey)
            If Key_Get Is Nothing Then
                Return False
            End If
            strValue = Key_Get.GetValue(strName) 'get the values of the key
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
    ''' <summary>
    ''' 判断值是否存在
    ''' </summary>
    ''' <param name="strKey"></param>
    ''' <param name="strName"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function RegistryExist(ByVal strKey As String, ByVal strName As String) As Boolean
        Try
            Dim Key_LocalMachine As RegistryKey
            Key_LocalMachine = My.Computer.Registry.LocalMachine 'get the key path of HKEY_LOCAL_MACHINE     
            Dim Key_Exist As RegistryKey
            Key_Exist = Key_LocalMachine.OpenSubKey(strKey, True) 'set the true for can write
            If Key_Exist Is Nothing Then
                Return False
            End If
            If Key_Exist.GetValue(strName) Is Nothing Then
                Return False
            End If
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
    ''' <summary>
    ''' 删除注册表对应值
    ''' </summary>
    ''' <param name="strKey"></param>
    ''' <param name="strName"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function DeleteRegistry(ByVal strKey As String, ByVal strName As String) As Boolean
        Try
            Dim Key_LocalMachine As RegistryKey
            Key_LocalMachine = My.Computer.Registry.LocalMachine 'get the key path of HKEY_LOCAL_MACHINE     
            Dim Key_Del As RegistryKey
            Key_Del = Key_LocalMachine.OpenSubKey(strKey, True) 'set the true for can write
            If Key_Del Is Nothing Then
                Return False
            End If
            Key_Del.DeleteValue(strName) 'delete the values of the key'name
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
End Class

代码2是https://blog.csdn.net/qq_33538554/article/details/97131174的VB版

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports Microsoft.Win32



Class RegistryHelper
    ''' <summary>
    ''' 读取指定名称的注册表的值
    ''' </summary>
    ''' <param name="name"></param>
    ''' <returns></returns>
    Public Function GetRegistryData(ByVal root As RegistryKey, ByVal subkey As String, ByVal name As String) As String
        Dim registData As String = ""
        Dim myKey As RegistryKey = root.OpenSubKey(subkey, True)
        If (Not (myKey) Is Nothing) Then
            registData = myKey.GetValue(name).ToString
        End If

        Return registData
    End Function

    ''' <summary>
    ''' 向注册表中写数据
    ''' </summary>
    ''' <param name="root"></param>
    ''' <param name="subkey"></param> 
    ''' <param name="name"></param> 
    ''' <param name="value"></param> 
    Public Sub SetRegistryData(ByVal root As RegistryKey, ByVal subkey As String, ByVal name As String, ByVal value As String)
        Dim aimdir As RegistryKey = root.CreateSubKey(subkey)
        aimdir.SetValue(name, value)
    End Sub

    ''' <summary>
    ''' 删除注册表中指定的注册表项
    ''' </summary>
    ''' <param name="name"></param>
    Public Sub DeleteRegist(ByVal root As RegistryKey, ByVal subkey As String, ByVal name As String)
        Dim subkeyNames() As String
        Dim myKey As RegistryKey = root.OpenSubKey(subkey, True)
        subkeyNames = myKey.GetSubKeyNames
        For Each aimKey As String In subkeyNames
            If (aimKey = name) Then
                myKey.DeleteSubKeyTree(name)
            End If

        Next
    End Sub
    ''' <summary>
    ''' 判断指定注册表项是否存在
    ''' </summary>
    ''' <param name="name"></param>
    ''' <returns></returns>
    Public Function IsRegistryExist(ByVal root As RegistryKey, ByVal subkey As String, ByVal name As String) As Boolean
        Dim _exit As Boolean = False
        Dim subkeyNames() As String
        Dim myKey As RegistryKey = root.OpenSubKey(subkey, True)
        subkeyNames = myKey.GetSubKeyNames
        For Each keyName As String In subkeyNames
            If (keyName = name) Then
                _exit = True
                Return _exit
            End If

        Next
        Return _exit
    End Function
End Class

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
VB编写注册表编辑器是一种功能强大的编程技术,可以对Windows系统中的注册表进行修改和管理。注册表是Windows系统的一个重要组成部分,包含了系统的配置信息和应用程序的设置。 在使用VB编写注册表编辑器时,首先需要使用编程语言提供的注册表类库或API函数来连接系统的注册表数据库。接下来,可以使用VB的用户界面设计工具创建一个注册表编辑器的界面,包括主窗口、菜单栏和工具栏等。 在注册表编辑器的界面中,可以显示注册表的层级结构,以树形或列表的形式展示不同的注册表项和键值。通过用户界面,可以实现对注册表的查看、添加、修改和删除等操作。 例如,用户可以选择一个注册表项,并在应用程序提供的输入框中输入新的键值,然后通过点击“添加”按钮将其添加到注册表中。同样,用户也可以选择一个已有的键值并进行修改或删除操作。 为了确保操作的安全性,注册表编辑器应该内置权限管理功能,例如管理员权限的验证和操作记录的日志功能。这样可以确保只有授权的用户可以对注册表进行修改,并且可以方便地追踪修改历史。 总的来说,VB编写注册表编辑器需要掌握VB编程语言的基本知识和系统注册表的结构和操作方式。合理利用编程语言提供的类库和API函数,结合用户界面设计和权限管理的要求,可以开发出一个功能完善的注册表编辑器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yueliang2100

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值