首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

访问注册表路径中包含""该如何避免

2012-01-26 
访问注册表路径中包含.,该如何处理各位大虾,我要访问注册表中某个键值,但是路径中包含如\Software\Micro

访问注册表路径中包含".",该如何处理
各位大虾,
我要访问注册表中某个键值,但是路径中包含如\Software\Microsoft\Office\12.0\MS   Project\Setting,包含   ". "的特殊符号,好像不能直接访问到,该如何处理啊?  



[解决办法]
Public Function GetKeyInfo(keyRoot As keyRoot, KeyName As String, SubKeyName() As String, ValueName() As String, ValueType() As ValueType, Optional CountKey As Long, Optional CountValue As Long, Optional MaxLenKey As Long, Optional MaxLenValue As Long) As Boolean
Dim f As FILETIME
Dim l As Long, s As String, strTmp As String, intTmp As Long

' 打开一个已存在的注册表关键字...
Success = RegOpenKeyEx(keyRoot, KeyName, 0, KEY_ALL_ACCESS, hKey)
If Success <> ERROR_SUCCESS Then GetKeyInfo = False: RegCloseKey hKey: Exit Function

' 获得一个已打开的注册表关键字的信息...
Success = RegQueryInfoKey(hKey, vbNullString, ByVal 0&, ByVal 0&, CountKey, MaxLenKey, ByVal 0&, CountValue, MaxLenValue, ByVal 0&, ByVal 0&, f)

If Success <> ERROR_SUCCESS Then GetKeyInfo = False: RegCloseKey hKey: Exit Function

If CountKey <> 0 Then
ReDim SubKeyName(CountKey - 1) As String ' 重新定义数组, 使用数组大小与注册表关键字的子项数量匹配
For i = 0 To CountKey - 1
strTmp = String(255, vbNullChar) 'Space(255)
l = 255
RegEnumKeyEx hKey, i, ByVal strTmp, l, 0, vbNullString, ByVal 0&, f
SubKeyName(i) = Left(strTmp, l)
If InStr(SubKeyName(i), vbNullChar) - 1 <> -1 Then
SubKeyName(i) = Left$(SubKeyName(i), InStr(SubKeyName(i), vbNullChar) - 1)
End If
Next i

' 下面的二重循环对字符串数组进行冒泡排序
For i = 0 To UBound(SubKeyName)
For j = i + 1 To UBound(SubKeyName)
If SubKeyName(i) > SubKeyName(j) Then
s = SubKeyName(i)
SubKeyName(i) = SubKeyName(j)
SubKeyName(j) = s
End If
Next j
Next i
End If

If CountValue <> 0 Then
ReDim ValueName(CountValue - 1) As String ' 重新定义数组, 使用数组大小与注册表关键字的子键数量匹配
ReDim ValueType(CountValue - 1) 'As Long ' 重新定义数组, 使用数组大小与注册表关键字的子键数量匹配
For i = 0 To CountValue - 1
strTmp = String(255, vbNullChar) 'Space(255)

l = 255
RegEnumValue hKey, i, ByVal strTmp, l, 0, intTmp, ByVal 0&, ByVal 0&
ValueType(i) = intTmp
ValueName(i) = Left(strTmp, l)
If InStr(ValueName(i), vbNullChar) - 1 <> -1 Then
ValueName(i) = Left$(ValueName(i), InStr(ValueName(i), vbNullChar) - 1)
End If
Next i

' 下面的二重循环对字符串数组进行冒泡排序
For i = 0 To UBound(ValueName)
For j = i + 1 To UBound(ValueName)
If ValueName(i) > ValueName(j) Then
s = ValueName(i)
ValueName(i) = ValueName(j)
ValueName(j) = s
End If
Next j
Next i
End If

' 关闭注册表关键字...
RegCloseKey hKey
GetKeyInfo = True ' 返回函数值
End Function

'-------------------------------------------------------------------------
'- 将 Double 型( 限制在 0--2^32-1 )的数字转换为十六进制并在前面补零
'- 参数说明: Number--要转换的 Double 型数字
'-------------------------------------------------------------------------


Private Function DoubleToHex(ByVal Number As Double) As String
Dim strHex As String
strHex = Space(8)
For i = 1 To 8
Select Case Number - Int(Number / 16) * 16
Case 10
Mid(strHex, 9 - i, 1) = "A "
Case 11
Mid(strHex, 9 - i, 1) = "B "
Case 12
Mid(strHex, 9 - i, 1) = "C "
Case 13
Mid(strHex, 9 - i, 1) = "D "
Case 14
Mid(strHex, 9 - i, 1) = "E "
Case 15
Mid(strHex, 9 - i, 1) = "F "
Case Else
Mid(strHex, 9 - i, 1) = CStr(Number - Int(Number / 16) * 16)
End Select
Number = Int(Number / 16)
Next i
DoubleToHex = strHex
End Function

Public Function RegDeleteSubkey(hKey As keyRoot, SubKey As String) As Boolean
'删除目录
'mhKey是指主键的名称,SubKey是指路径
Dim ret As Long, Index As Long, hName As String
Dim hSubkey As Long
ret = RegOpenKey(hKey, SubKey, hSubkey)
If ret <> 0 Then
RegDeleteSubkey = False
Exit Function
End If
ret = RegDeleteKey(hSubkey, " ")
If ret <> 0 Then '如果删除失败则认为是NT则用递归方法删除目录
hName = String(256, Chr(0))
While RegEnumKey(hSubkey, 0, hName, Len(hName)) = 0 And _
RegDeleteSubkey(hSubkey, hName)
Wend
ret = RegDeleteKey(hSubkey, " ")
End If
RegDeleteSubkey = (ret = 0)
RegCloseKey hSubkey '删除打开的键值,释放内存
End Function

Public Function RegDeleteKeyName(mhKey As keyRoot, SubKey As String, hKeyName As String) As Boolean
'删除子键数据
'mhKey是指主键的名称,SubKey是指路径,hKeyName是指键名
Dim hKey As Long, ret As Long
ret = RegOpenKey(mhKey, SubKey, hKey)
RegDeleteKeyName = False
If ret = 0 Then
If RegDeleteValue(hKey, hKeyName) = 0 Then RegDeleteKeyName = True
End If
RegCloseKey hKey '删除打开的键值,释放内存
End Function

用GetKeyValue这个函数就可以了

热点排行