检查WScript是否存在注册表项

kis*_*sin 4 registry wsh

我试图检查是否存在注册表项,无论我尝试什么,我总是收到错误消息"无法打开注册表项进行阅读"

我正在使用的代码:

keyPath = "HKEY_LOCAL_MACHINE\\SOFTWARE\\BOS\\BOSaNOVA TCP/IP\\Set 1\\Cfg\\Sign On\\";

try
{
    var shell = new ActiveXObject("WScript.Shell");
    var regValue = shell.RegRead(keyPath);

    shell = null;
}
catch(err)
{

}
Run Code Online (Sandbox Code Playgroud)

我在这里失踪了什么?

Red*_*ter 7

您可能需要删除尾部斜杠.如果您使用它,它将查找您指定的键的默认值,如果找不到它,将给出该错误.

相反,如果您尝试通过不使用尾部斜杠来访问密钥,就好像它是一个值,您将得到相同的错误.

尝试访问密钥的一些示例:

失败:

var keyPath = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion";
var shell = new ActiveXObject("WScript.Shell");
var regValue = shell.RegRead(keyPath);
WScript.Echo("Value: " + regValue);
Run Code Online (Sandbox Code Playgroud)

成功(但由于默认值为空,因此给出空结果):

var keyPath = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\";
var shell = new ActiveXObject("WScript.Shell");
var regValue = shell.RegRead(keyPath);
WScript.Echo("Value: " + regValue);
Run Code Online (Sandbox Code Playgroud)

尝试访问值的一些示例:

成功(输出Value: C:\Program Files):

var keyPath = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\ProgramFilesDir";
var shell = new ActiveXObject("WScript.Shell");
var regValue = shell.RegRead(keyPath);
WScript.Echo("Value: " + regValue);
Run Code Online (Sandbox Code Playgroud)

失败(访问值时不应使用尾部斜杠):

var keyPath = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\ProgramFilesDir\\";
var shell = new ActiveXObject("WScript.Shell");
var regValue = shell.RegRead(keyPath);
WScript.Echo("Value: " + regValue);
Run Code Online (Sandbox Code Playgroud)