VB NET 注册表操作

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
发布了254 篇原创文章 · 获赞 31 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/yueliang2100/article/details/103442221