VB.NET存取注册表项的几个函数

18 篇文章 0 订阅
16 篇文章 0 订阅


做了几个增删改注册表项的函数,需要时可以拷贝来用:

Imports Microsoft.Win32 ' provided by mscorlib.dll

Public Class RegistryAccess

    Public Shared Function GetRegistryKey(ByVal registryRoot As RegistryKey, ByVal keyName As String, ByVal valueName As String) As Object
        '
        ' registryKey = Microsoft.Win32.Registry.LocalMachine | .ClassesRoot | .CurrentConfig | .CurrentUser | .DynData | .PerformanceData | .Users
        ' keyName = "Software//Microsoft//IE Setup/Setup"
        ' valueName = "Path"
        '
        Dim subKey As RegistryKey
        subKey = registryRoot.OpenSubKey(keyName, False)
        Dim valueData As Object
        If (Not subKey Is Nothing) Then
            valueData = subKey.GetValue(valueName)
            subKey.Close()
        End If
        Return valueData

    End Function

    Public Shared Function SetRegistryKey(ByVal registryRoot As RegistryKey, ByVal keyName As String, ByVal valueName As String, ByVal valueData As Object) As Boolean
        '
        Dim subKey As RegistryKey
        subKey = registryRoot.OpenSubKey(keyName, True)
        If subKey Is Nothing Then
            ' Create it if it doesn't exist
            subKey = registryRoot.CreateSubKey(keyName)
        End If

        If (Not subKey Is Nothing) Then
            subKey.SetValue(valueName, valueData)
            subKey.Close()
        Else
            ' Failure
            Return False
        End If
        Return True
    End Function

    Public Shared Function DeleteRegistryKey(ByVal registryRoot As RegistryKey, ByVal keyName As String, Optional ByVal ErrorOnFail As Boolean = False) As Boolean
        '
        Try
            registryRoot.DeleteSubKeyTree(keyName)
            Return True
        Catch ex As Exception
            If ErrorOnFail Then
                Throw ex
            Else
                Return False
            End If
        End Try

    End Function

End Class
------------------
调用方法示例:
RegistryAccess.SetRegistryKey(Microsoft.Win32.Registry.LocalMachine, "Software//Company Name//Section Name//Key Name", "ValueName", "ValueData")

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值