2哥学Vb.net--注册表操作

对注册表的操作:简单的增删改查

Imports Microsoft.Win32
   
Public Class Form1
    Private RegCurrentConfig As RegistryHive = "-2147483643"
    Private RegClassUsers As RegistryHive = "-2147483645"
    Private RegLocalMachine As RegistryHive = "-2147483646"
    Private RegCurrentUser As RegistryHive = "-2147483647"
    Private RegClassRoot As RegistryHive = "-2147483648"
    ''' <summary>
    ''' 操作注册表
    ''' </summary>
    ''' <param name="Hive"></param>
    ''' <param name="Key"></param>
    ''' <param name="ValueName"></param>
    ''' <param name="ErroInfo"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function RegValue(ByVal Hive As RegistryHive, ByVal Key As String, ByVal ValueName As String, Optional ByRef ErroInfo As String = "") As String
        RegValue = ""
        Dim objParentKey As RegistryKey
        Dim objSubKey As RegistryKey

        Select Case Hive
            Case RegistryHive.ClassesRoot
                objParentKey = Registry.ClassesRoot
            Case RegistryHive.CurrentConfig
                objParentKey = Registry.CurrentConfig
            Case RegistryHive.CurrentUser
                objParentKey = Registry.CurrentUser
            Case RegistryHive.LocalMachine
                objParentKey = Registry.PerformanceData
            Case RegistryHive.Users
                objParentKey = Registry.Users
        End Select

        'Create subkey
        Dim rk As RegistryKey
        rk = objParentKey.OpenSubKey("Hello", True)
        rk.CreateSubKey("MyRegistrySubKey", True)

        'Set value
        rk.SetValue("Hello", "Kitty")

        'get Value
        Try
            objSubKey = objParentKey.OpenSubKey(Key)
            If IsNothing(objSubKey) = False Then
                RegValue = (objSubKey.GetValue(ValueName))
            End If
        Catch ex As Exception
            ErroInfo = ex.Message
        Finally
            If ErroInfo = "" And RegValue = "" Then
                ErroInfo = "No value"
            End If
        End Try

        'delete subkey
        rk.DeleteSubKey("xxx", False)

        rk.Close()
        Return RegValue
    End Function


    'test
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MsgBox(RegValue(RegCurrentUser, "Control Panel\Colors", "ActiveBorder"))
    End Sub
End Class

更加详细的信息,可以查询:http://www.lob.cn/Special/registry/Index.shtml

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值