Read and Write Registry on both Windows 7 & 10

Read and Write Registry on both Windows 7 & 10

Windows 7:

    Private Function GetRegKeyValue32(sFullKeyPath As String, sKeyName As String) As String
        Return My.Computer.Registry.GetValue(sFullKeyPath, sKeyName, "")
    End Function

    Private Sub SetRegKeyValue32(sFullKeyPath As String, sKeyName As String, sValue As String)
        My.Computer.Registry.SetValue(sFullKeyPath, sKeyName, sValue)
    End Sub

Windows 10:

    Private Function GetRegKeyValue(eRegHive As RegistryHive, sSubKeyPath As String, sKeyName As String) As String
        Dim baseReg As RegistryKey = RegistryKey.OpenBaseKey(eRegHive, RegistryView.Registry64)
        Dim subReg As RegistryKey = baseReg.OpenSubKey(sSubKeyPath, True)
        Return subReg.GetValue(sKeyName, "", RegistryValueKind.String)
    End Function

    Private Sub SetRegKeyValue(eRegHive As RegistryHive, sSubKeyPath As String, sKeyName As String, sValue As String)
        Dim baseReg As RegistryKey = RegistryKey.OpenBaseKey(eRegHive, RegistryView.Registry64)
        Dim subReg As RegistryKey = baseReg.OpenSubKey(sSubKeyPath, True)
        subReg.SetValue(sKeyName, sValue, RegistryValueKind.String)
    End Sub

Test codes:
    Const HKLM As String = "HKEY_LOCAL_MACHINE\"
    Const AbcSubKey As String = "SOFTWARE\ABC"
    Dim sProductName As String = GetRegKeyValue32(HKLM & AbcSubKey , "ProductName")
    SetRegKeyValue32(HKLM & AbcSubKey , "ProductName", "Abc Blah Blah")

Comments

Popular posts from this blog

Error - vbe6ext.olb could not be loaded

Visual Studio $(TargetDir) macro is empty