VB判断是否存在一个注册表子键

比如要判断是否存在"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced\Folder\Hidden\SHOWALL\"下的"CheckedValue",请问该怎么写代码?
(注意,存在就msgbox "true")

Sub t()
'要查询的注册表路径
Key = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced\Folder\Hidden\SHOWALL"
FindValue = "CheckedValue" '要查询的注册表值
TempFile = Environ("TEMP") & "\r.txt" '临时文件名
'利用命令提示符和reg命令执行注册表查询并输出到临时文件
Shell "cmd.exe /c reg.exe query " & Key & " > " & TempFile
While Dir(TempFile) = "" '查看临时文件是否生成
DoEvents
Wend
For i = 1 To 10000 '延时
DoEvents
Next
Open TempFile For Input As #1 '打开临时文件
S = Input(LOF(1), #1) '读取临时文件内容
Close #1 '关闭临时文件
If InStr(1, S, FindValue) <> 0 Then MsgBox "True" '判断是否存在要查询的注册表值
Kill TempFile ' '删除临时文件
End Sub
温馨提示:内容为网友见解,仅供参考
第1个回答  2009-01-24
RegCreateKeyEx
如果键不存在,最后一个参数会返回
REG_CREATED_NEW_KEY,如果存在,会返回REG_OPENED_EXISTING_KEY
第2个回答  2009-02-04
代码如下。
===============================
Option Explicit
Dim hR As Long
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long ' Note that if you declare the lpData parameter as String, you must pass it By Value.

Private Sub Command1_Click()
RegOpenKey HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced\Folder\Hidden\SHOWALL", hR '这里是项名
Dim r As Long
r = RegQueryValueEx(hR, "CheckedValue", 0, 0, 0, 0)
If r = 2 Then
MsgBox "不存在"
Else
MsgBox "存在"
End If
End Sub

Warning: Invalid argument supplied for foreach() in /www/wwwroot/www.t2y.org3v3b34/skin/templets/default/contents.html on line 47
相似回答
大家正在搜