小弟写了一个注册表操作的类,请那位大虾改进一下 Class RegEditor Public Function RegWrite(ByVal RegPath As String, ByVal Value As Object) As Boolean RegWrite = True Try Dim s() As String = Split(RegPath, "\") Dim HKCR As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.ClassesRoot Dim HKCU As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser Dim HKLM As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine Dim HKUS As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.Users Dim HKCC As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentConfig Dim HKDD As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.DynData Dim HKPD As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.PerformanceData Dim HKEY As Microsoft.Win32.RegistryKey If s.Length > 2 Then Select Case s(0).ToUpper Case "HKCR", "HKEY_CLASSES_ROOT" HKEY = HKCR.CreateSubKey(s(1)) 'HKEY = HKCR.OpenSubKey(s(1), True) Case "HKCU", "HKEY_CURRENT_USER" HKEY = HKCU.CreateSubKey(s(1)) 'HKEY = HKCU.OpenSubKey(s(1), True) Case "HKLM", "HKEY_LOCAL_MACHINE" HKEY = HKLM.CreateSubKey(s(1)) 'HKEY = HKLM.OpenSubKey(s(1), True) Case "HKUS", "HKEY_USERS" HKEY = HKUS.CreateSubKey(s(1)) 'HKEY = HKUS.OpenSubKey(s(1), True) Case "HKCC", "HKEY_CURRENT_CONFIG" HKEY = HKCC.CreateSubKey(s(1)) 'HKEY = HKCC.OpenSubKey(s(1), True) Case "HKDD", "HKEY_DYN_DATA" HKEY = HKDD.CreateSubKey(s(1)) 'HKEY = HKDD.OpenSubKey(s(1), True) Case "HKPD", "HKEY_PERFORMANCE_DATA" HKEY = HKPD.CreateSubKey(s(1)) 'HKEY = HKPD.OpenSubKey(s(1), True) End Select Else Select Case s(0).ToUpper Case "HKCR", "HKEY_CLASSES_ROOT" HKEY = HKCR Case "HKCU", "HKEY_CURRENT_USER" HKEY = HKCU Case "HKLM", "HKEY_LOCAL_MACHINE" HKEY = HKLM Case "HKUS", "HKEY_USERS" HKEY = HKUS Case "HKCC", "HKEY_CURRENT_CONFIG" HKEY = HKCC Case "HKDD", "HKEY_DYN_DATA" HKEY = HKDD Case "HKPD", "HKEY_PERFORMANCE_DATA" HKEY = HKPD End Select End If Dim i As Integer For i = 2 To s.Length - 2 HKEY = HKEY.CreateSubKey(s(i)) 'HKEY = HKEY.OpenSubKey(s(i), True) Next HKEY.SetValue(s(s.Length - 1), Value) HKEY.Close() HKCR.Close() HKCU.Close() HKLM.Close() HKUS.Close() HKCC.Close() HKDD.Close() HKPD.Close() Catch ex As Exception Return False End Try End Function
Public Function RegRead(ByVal RegPath As String, ByVal DefaultValue As Object) Try Dim s() As String = Split(RegPath, "\") Dim HKCR As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.ClassesRoot Dim HKCU As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser Dim HKLM As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine Dim HKUS As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.Users Dim HKCC As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentConfig Dim HKDD As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.DynData Dim HKPD As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.PerformanceData Dim HKEY As Microsoft.Win32.RegistryKey If s.Length > 2 Then Select Case s(0).ToUpper Case "HKCR", "HKEY_CLASSES_ROOT" HKEY = HKCR.OpenSubKey(s(1)) Case "HKCU", "HKEY_CURRENT_USER" HKEY = HKCU.OpenSubKey(s(1)) Case "HKLM", "HKEY_LOCAL_MACHINE" HKEY = HKLM.OpenSubKey(s(1)) Case "HKUS", "HKEY_USERS" HKEY = HKUS.OpenSubKey(s(1)) Case "HKCC", "HKEY_CURRENT_CONFIG" HKEY = HKCC.OpenSubKey(s(1)) Case "HKDD", "HKEY_DYN_DATA" HKEY = HKDD.OpenSubKey(s(1)) Case "HKPD", "HKEY_PERFORMANCE_DATA" HKEY = HKPD.OpenSubKey(s(1)) End Select Else Select Case s(0).ToUpper Case "HKCR", "HKEY_CLASSES_ROOT" HKEY = HKCR Case "HKCU", "HKEY_CURRENT_USER" HKEY = HKCU Case "HKLM", "HKEY_LOCAL_MACHINE" HKEY = HKLM Case "HKUS", "HKEY_USERS" HKEY = HKUS Case "HKCC", "HKEY_CURRENT_CONFIG" HKEY = HKCC Case "HKDD", "HKEY_DYN_DATA" HKEY = HKDD Case "HKPD", "HKEY_PERFORMANCE_DATA" HKEY = HKPD End Select End If Dim i As Integer For i = 2 To s.Length - 2 HKEY = HKEY.OpenSubKey(s(i)) Next Return HKEY.GetValue(s(s.Length - 1), DefaultValue) Catch ex As Exception Return DefaultValue End Try End Function
Public Function RegDelete(ByVal RegPath As String) As Boolean RegDelete = True Try Dim s() As String = Split(RegPath, "\") Dim HKCR As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.ClassesRoot Dim HKCU As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser Dim HKLM As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine Dim HKUS As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.Users Dim HKCC As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentConfig Dim HKDD As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.DynData Dim HKPD As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.PerformanceData Dim HKEY As Microsoft.Win32.RegistryKey If s.Length > 2 Then Select Case s(0).ToUpper Case "HKCR", "HKEY_CLASSES_ROOT" HKEY = HKCR.OpenSubKey(s(1), True) Case "HKCU", "HKEY_CURRENT_USER" HKEY = HKCU.OpenSubKey(s(1), True) Case "HKLM", "HKEY_LOCAL_MACHINE" HKEY = HKLM.OpenSubKey(s(1), True) Case "HKUS", "HKEY_USERS" HKEY = HKUS.OpenSubKey(s(1), True) Case "HKCC", "HKEY_CURRENT_CONFIG" HKEY = HKCC.OpenSubKey(s(1), True) Case "HKDD", "HKEY_DYN_DATA" HKEY = HKDD.OpenSubKey(s(1), True) Case "HKPD", "HKEY_PERFORMANCE_DATA" HKEY = HKPD.OpenSubKey(s(1), True) End Select Else Select Case s(0).ToUpper Case "HKCR", "HKEY_CLASSES_ROOT" HKEY = HKCR Case "HKCU", "HKEY_CURRENT_USER" HKEY = HKCU Case "HKLM", "HKEY_LOCAL_MACHINE" HKEY = HKLM Case "HKUS", "HKEY_USERS" HKEY = HKUS Case "HKCC", "HKEY_CURRENT_CONFIG" HKEY = HKCC Case "HKDD", "HKEY_DYN_DATA" HKEY = HKDD Case "HKPD", "HKEY_PERFORMANCE_DATA" HKEY = HKPD End Select End If Dim i As Integer For i = 2 To s.Length - 2 HKEY = HKEY.OpenSubKey(s(i), True) Next HKEY.DeleteValue(s(s.Length - 1)) Catch ex As Exception Return False End Try End Function
Public Function RegDeleteSubKey(ByVal RegPath As String, Optional ByVal DeleteSubKey As Boolean = False) As Boolean RegDeleteSubKey = True Try Dim s() As String = Split(RegPath, "\") Dim HKCR As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.ClassesRoot Dim HKCU As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser Dim HKLM As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine Dim HKUS As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.Users Dim HKCC As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentConfig Dim HKDD As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.DynData Dim HKPD As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.PerformanceData Dim HKEY As Microsoft.Win32.RegistryKey If s.Length > 2 Then Select Case s(0).ToUpper Case "HKCR", "HKEY_CLASSES_ROOT" HKEY = HKCR.OpenSubKey(s(1), True) Case "HKCU", "HKEY_CURRENT_USER" HKEY = HKCU.OpenSubKey(s(1), True) Case "HKLM", "HKEY_LOCAL_MACHINE" HKEY = HKLM.OpenSubKey(s(1), True) Case "HKUS", "HKEY_USERS" HKEY = HKUS.OpenSubKey(s(1), True) Case "HKCC", "HKEY_CURRENT_CONFIG" HKEY = HKCC.OpenSubKey(s(1), True) Case "HKDD", "HKEY_DYN_DATA" HKEY = HKDD.OpenSubKey(s(1), True) Case "HKPD", "HKEY_PERFORMANCE_DATA" HKEY = HKPD.OpenSubKey(s(1), True) End Select Else Select Case s(0).ToUpper Case "HKCR", "HKEY_CLASSES_ROOT" HKEY = HKCR Case "HKCU", "HKEY_CURRENT_USER" HKEY = HKCU Case "HKLM", "HKEY_LOCAL_MACHINE" HKEY = HKLM Case "HKUS", "HKEY_USERS" HKEY = HKUS Case "HKCC", "HKEY_CURRENT_CONFIG" HKEY = HKCC Case "HKDD", "HKEY_DYN_DATA" HKEY = HKDD Case "HKPD", "HKEY_PERFORMANCE_DATA" HKEY = HKPD End Select End If Dim i As Integer For i = 2 To s.Length - 2 HKEY = HKEY.OpenSubKey(s(i), True) Next If DeleteSubKey Then HKEY.DeleteSubKeyTree(s(s.Length - 1)) Else HKEY.DeleteSubKey(s(s.Length - 1), True) Catch ex As Exception Return False End Try End Function
Public Function RegCreateSubKey(ByVal RegPath As String) As Boolean RegCreateSubKey = True Try Dim s() As String = Split(RegPath, "\") Dim HKCR As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.ClassesRoot Dim HKCU As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser Dim HKLM As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine Dim HKUS As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.Users Dim HKCC As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentConfig Dim HKDD As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.DynData Dim HKPD As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.PerformanceData Dim HKEY As Microsoft.Win32.RegistryKey If s.Length > 2 Then Select Case s(0).ToUpper Case "HKCR", "HKEY_CLASSES_ROOT" HKEY = HKCR.CreateSubKey(s(1)) 'HKEY = HKCR.OpenSubKey(s(1), True) Case "HKCU", "HKEY_CURRENT_USER" HKEY = HKCU.CreateSubKey(s(1)) 'HKEY = HKCU.OpenSubKey(s(1), True) Case "HKLM", "HKEY_LOCAL_MACHINE" HKEY = HKLM.CreateSubKey(s(1)) 'HKEY = HKLM.OpenSubKey(s(1), True) Case "HKUS", "HKEY_USERS" HKEY = HKUS.CreateSubKey(s(1)) 'HKEY = HKUS.OpenSubKey(s(1), True) Case "HKCC", "HKEY_CURRENT_CONFIG" HKEY = HKCC.CreateSubKey(s(1)) 'HKEY = HKCC.OpenSubKey(s(1), True) Case "HKDD", "HKEY_DYN_DATA" HKEY = HKDD.CreateSubKey(s(1)) 'HKEY = HKDD.OpenSubKey(s(1), True) Case "HKPD", "HKEY_PERFORMANCE_DATA" HKEY = HKPD.CreateSubKey(s(1)) 'HKEY = HKPD.OpenSubKey(s(1), True) End Select Else Select Case s(0).ToUpper Case "HKCR", "HKEY_CLASSES_ROOT" HKEY = HKCR Case "HKCU", "HKEY_CURRENT_USER" HKEY = HKCU Case "HKLM", "HKEY_LOCAL_MACHINE" HKEY = HKLM Case "HKUS", "HKEY_USERS" HKEY = HKUS Case "HKCC", "HKEY_CURRENT_CONFIG" HKEY = HKCC Case "HKDD", "HKEY_DYN_DATA" HKEY = HKDD Case "HKPD", "HKEY_PERFORMANCE_DATA" HKEY = HKPD End Select End If Dim i As Integer For i = 2 To s.Length - 2 HKEY = HKEY.CreateSubKey(s(i)) Next HKEY.CreateSubKey(s(s.Length - 1)) Catch ex As Exception Return False End Try End Function End Class