Imports Microsoft.Win32
Public Class MyReG
    Enum RegRootkey
        CURRENT_USER = 1
        LOCAL_MACHINE = 2
        CLASSES_ROOT = 3
        USERS = 4
        CURRENT_CONFIG = 5
    End Enum
    Public Function Read(ByVal rootkey As RegRootkey, ByVal subkey As String, ByVal ValName As String) As Object
        Dim RegKey As RegistryKey
        Dim Result As Object

        Select Case rootkey
            Case RegRootkey.CURRENT_USER
                RegKey = Registry.CurrentUser.OpenSubKey(subkey, True)
            Case RegRootkey.LOCAL_MACHINE
                RegKey = Registry.LocalMachine.OpenSubKey(subkey, True)
            Case RegRootkey.CLASSES_ROOT
                RegKey = Registry.ClassesRoot.OpenSubKey(subkey, True)
            Case RegRootkey.USERS
                RegKey = Registry.Users.OpenSubKey(subkey, True)
            Case RegRootkey.CURRENT_CONFIG
                RegKey = Registry.CurrentConfig.OpenSubKey(subkey, True)
            Case Else
                Return Nothing
        End Select

        If Not RegKey Is Nothing Then
            Result = RegKey.GetValue(ValName, Nothing)
            RegKey.Close()
            Return Result
        Else '//첫 실행일때다 즉 값이 없을때
            Return Nothing
        End If
    End Function
    Public Function Write(ByVal rootkey As RegRootkey, ByVal subkey As String, ByVal ValName As String, ByVal ValVal As String) As Boolean
        Dim RegKey As RegistryKey
        ' Dim Result As Object
        Select Case rootkey
            Case RegRootkey.CURRENT_USER
                RegKey = Registry.CurrentUser.OpenSubKey(subkey, True)
            Case RegRootkey.LOCAL_MACHINE
                RegKey = Registry.LocalMachine.OpenSubKey(subkey, True)
            Case RegRootkey.CLASSES_ROOT
                RegKey = Registry.ClassesRoot.OpenSubKey(subkey, True)
            Case RegRootkey.USERS
                RegKey = Registry.Users.OpenSubKey(subkey, True)
            Case RegRootkey.CURRENT_CONFIG
                RegKey = Registry.CurrentConfig.OpenSubKey(subkey, True)
            Case Else
                Return False
        End Select

        If RegKey Is Nothing Then
            Select Case rootkey
                Case RegRootkey.CURRENT_USER
                    RegKey = Registry.CurrentUser.CreateSubKey(subkey)
                Case RegRootkey.LOCAL_MACHINE
                    RegKey = Registry.LocalMachine.CreateSubKey(subkey)
                Case RegRootkey.CLASSES_ROOT
                    RegKey = Registry.ClassesRoot.CreateSubKey(subkey)
                Case RegRootkey.USERS
                    RegKey = Registry.Users.CreateSubKey(subkey)
                Case RegRootkey.CURRENT_CONFIG
                    RegKey = Registry.CurrentConfig.CreateSubKey(subkey)
                Case Else
                    Return False
            End Select
        End If
        RegKey.SetValue(ValName, ValVal)
        RegKey.Close()
        Return True
    End Function
    Public Function DeleteKey(ByVal rootkey As RegRootkey, ByVal subkey As String) As Boolean
        '//잘 작동함
        Select Case rootkey
            Case RegRootkey.CURRENT_USER
                Registry.CurrentUser.DeleteSubKeyTree(subkey)
            Case RegRootkey.LOCAL_MACHINE
                Registry.LocalMachine.DeleteSubKeyTree(subkey)
            Case RegRootkey.CLASSES_ROOT
                Registry.ClassesRoot.DeleteSubKeyTree(subkey)
            Case RegRootkey.USERS
                Registry.Users.DeleteSubKeyTree(subkey)
            Case RegRootkey.CURRENT_CONFIG
                Registry.CurrentConfig.DeleteSubKeyTree(subkey)
            Case Else
                Return False
        End Select
        Return True
    End Function
    Public Function DeleteVal(ByVal rootkey As RegRootkey, ByVal subkey As String, ByVal ValName As String) As Boolean
        Dim RegKey As RegistryKey
        ' Dim Result As Object
        Select Case rootkey
            Case RegRootkey.CURRENT_USER
                RegKey = Registry.CurrentUser.OpenSubKey(subkey, True)
            Case RegRootkey.LOCAL_MACHINE
                RegKey = Registry.LocalMachine.OpenSubKey(subkey, True)
            Case RegRootkey.CLASSES_ROOT
                RegKey = Registry.ClassesRoot.OpenSubKey(subkey, True)
            Case RegRootkey.USERS
                RegKey = Registry.Users.OpenSubKey(subkey, True)
            Case RegRootkey.CURRENT_CONFIG
                RegKey = Registry.CurrentConfig.OpenSubKey(subkey, True)
            Case Else
                Return False
        End Select

        RegKey.DeleteValue(ValName)
        RegKey.Close()
        Return True
    End Function
End Class