| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1161 人关注过本帖
标题:[原创]小弟写了一个注册表操作的类,请那位大虾改进一下
只看楼主 加入收藏
xhwxwk
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2005-8-24
收藏
 问题点数:0 回复次数:1 
[原创]小弟写了一个注册表操作的类,请那位大虾改进一下

小弟写了一个注册表操作的类,请那位大虾改进一下 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

搜索更多相关主题的帖子: 注册表 改进 
2005-08-25 09:46
plggli
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2006-3-16
收藏
得分:0 
象一坨大便,也不加整理一下
2010-07-06 10:46
快速回复:[原创]小弟写了一个注册表操作的类,请那位大虾改进一下
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035598 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved