可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I thought this would be easy, but apparently nobody does it... I'm trying to see if a registry key exists. I don't care if there are any values inside of it such as (Default).
This is what I've been trying.
Set objRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv") objRegistry.GetStringValue &H80000003,".DEFAULT\Network","",regValue If IsEmpty(regValue) Then Wscript.Echo "The registry key does not exist." Else Wscript.Echo "The registry key exists." End If
I only want to know if HKEY_USERES\.DEFAULT\.Network exists. Anything I find when searching mostly seems to discuss manipulating them and pretty much assumes the key does exists since it's magically created if it doesn't.
回答1:
I found the solution.
dim bExists ssig="Unable to open registry key" set wshShell= Wscript.CreateObject("WScript.Shell") strKey = "HKEY_USERS\.Default\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Digest\" on error resume next present = WshShell.RegRead(strKey) if err.number0 then if right(strKey,1)="\" then 'strKey is a registry key if instr(1,err.description,ssig,1)0 then bExists=true else bExists=false end if else 'strKey is a registry valuename bExists=false end if err.clear else bExists=true end if on error goto 0 if bExists=vbFalse then wscript.echo strKey & " does not exist." else wscript.echo strKey & " exists." end if
回答2:
The second of the two methods here does what you're wanting. I've just used it (after finding no success in this thread) and it's worked for me.
http://yorch.org/2011/10/two-ways-to-check-if-a-registry-key-exists-using-vbscript/
The code:
回答3:
Simplest way avoiding RegRead and error handling tricks. Optional friendly consts for the registry:
Then check with:
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv") If oReg.EnumKey(HKEY_LOCAL_MACHINE, "SYSTEM\Example\Key\", "", "") = 0 Then MsgBox "Key Exists" Else MsgBox "Key Not Found" End If
IMPORTANT NOTES FOR THE ABOVE:
- There are 4 parameters being passed to EnumKey, not the usual 3.
- Equals zero means the key EXISTS.
- The slash after key name is optional and not required.
回答4:
In case anyone else runs into this, I took WhoIsRich's example and modified it a bit. When calling ReadReg I needed to do the following: ReadReg("App", "HKEY_CURRENT_USER\App\Version") which would then be able to read the version number from the registry, if it existed. I also am using HKCU since it does not require admin privileges to write to.
Function ReadReg(RegKey, RegPath) Const HKEY_CURRENT_USER = &H80000001 Dim objRegistry, oReg Set objRegistry = CreateObject("Wscript.shell") Set oReg = GetObject("winmgmts:!root\default:StdRegProv") if oReg.EnumKey(HKEY_CURRENT_USER, RegKey) = 0 Then ReadReg = objRegistry.RegRead(RegPath) else ReadReg = "" end if End Function
回答5:
edit (sorry I thought you wanted VBA).
Anytime you try to read a non-existent value from the registry, you get back a Null. Thus all you have to do is check for a Null value.
Use IsNull
not IsEmpty
.
Const HKEY_LOCAL_MACHINE = &H80000002 strComputer = "." Set objRegistry = GetObject("winmgmts:\\" & _ strComputer & "\root\default:StdRegProv") strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion" strValueName = "Test Value" objRegistry.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue If IsNull(strValue) Then Wscript.Echo "The registry key does not exist." Else Wscript.Echo "The registry key exists." End If
回答6:
See the Scripting Guy! Blog:
How Can I Tell Whether a Value Exists in the Registry?
They discuss doing the check on a remote computer and show that if you read a string value from the key, and if the value is Null (as opposed to Empty), the key does not exist.
With respect to using the RegRead method, if the term "key" refers to the path (or folder) where registry values are kept, and if the leaf items in that key are called "values", using WshShell.RegRead(strKey) to detect key existence (as opposed to value existance) consider the following (as observed on Windows XP):
If strKey name is not the name of an existing registry path, Err.Description reads "Invalid root in registry key"... with an Err.Number of 0x80070002.
If strKey names a registry path that exists but does not include a trailing "\" the RegRead method appears to interpret strKey as a path\value reference rather than as a simple path reference, and returns the same Err.Number but with an Err.Description of "Unable to open registry key". The term "key" in the error message appears to mean "value". This is the same result obtained when strKey references a path\value where the path exists, but the value does not exist.