windows 使用 VBScript 检查注册表项是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9604276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:20:11  来源:igfitidea点击:

Check if registry key exists using VBScript

windowsvbscriptregistry

提问by MTeck

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.

我只想知道 HKEY_USERES\.DEFAULT\.Network 是否存在。我在搜索时发现的任何东西似乎都在讨论操纵它们,并且几乎假设密钥确实存在,因为如果不存在,它就会神奇地创建。

采纳答案by MTeck

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.number<>0 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

回答by chris_k

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/

http://yorch.org/2011/10/two-ways-to-check-if-a-registry-key-exists-using-vbscript/

The code:

编码:

Const?HKCR?=?&H80000000?'HKEY_CLASSES_ROOT
Const?HKCU?=?&H80000001?'HKEY_CURRENT_USER
Const?HKLM?=?&H80000002?'HKEY_LOCAL_MACHINE
Const?HKUS?=?&H80000003?'HKEY_USERS
Const?HKCC?=?&H80000005?'HKEY_CURRENT_CONFIG

Function?KeyExists(Key,?KeyPath)
    Dim?oReg: Set?oReg?=?GetObject("winmgmts:!root/default:StdRegProv")
    If?oReg.EnumKey(Key,?KeyPath,?arrSubKeys)?=?0?Then
        KeyExists?=?True
    Else
        KeyExists?=?False
   End?If
End?Function

回答by WhoIsRich

Simplest way avoiding RegRead and error handling tricks. Optional friendly consts for the registry:

避免 RegRead 和错误处理技巧的最简单方法。注册表的可选友好常量:

Const?HKEY_CLASSES_ROOT?  =?&H80000000
Const?HKEY_CURRENT_USER?  =?&H80000001
Const?HKEY_LOCAL_MACHINE ?=?&H80000002
Const?HKEY_USERS?         =?&H80000003
Const?HKEY_CURRENT_CONFIG?=?&H80000005

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.
  • 传递给 EnumKey 的参数有 4 个,而不是通常的 3 个。
  • 等于零表示键存在。
  • 键名后面的斜杠是可选的,不是必需的。

回答by Jonathan

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.

以防其他人遇到这个问题,我采用了 WhoIsRich 的示例并对其进行了一些修改。调用 ReadReg 时,我需要执行以下操作: ReadReg("App", "HKEY_CURRENT_USER\App\Version") 然后可以从注册表中读取版本号(如果存在)。我也在使用 HKCU,因为它不需要管理员权限来写入。

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

回答by vulkanino

edit (sorry I thought you wanted VBA).

编辑(对不起,我以为你想要 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.

每当您尝试从注册表中读取不存在的值时,您都会返回 Null。因此,您所要做的就是检查 Null 值。

Use IsNullnot IsEmpty.

使用IsNullIsEmpty

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

回答by kbulgrien

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.

他们讨论在远程计算机上进行检查,并表明如果您从键中读取字符串值,并且该值为 Null(与 Empty 相对),则键不存在。

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):

关于使用 RegRead 方法,如果术语“键”是指保存注册表值的路径(或文件夹),并且如果该键中的叶项称为“值”,则使用 WshShell.RegRead(strKey)检测键存在(相对于值存在)考虑以下(如在 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.

如果 strKey 名称不是现有注册表路径的名称,则 Err.Description 将读取“注册表项中的根无效”...,其 Err.Number 为 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.

如果 strKey 命名的注册表路径存在但不包含尾随的“\”,则 RegRead 方法似乎将 strKey 解释为路径\值引用而不是简单的路径引用,并返回相同的 Err.Number 但带有 Err。 “无法打开注册表项”的描述。错误消息中的术语“键”似乎表示“值”。这与 strKey 引用路径存在但值不存在的路径\值时获得的结果相同。