检查 WScript 是否存在注册表项 [英] check if a registry key exists with WScript

查看:107
本文介绍了检查 WScript 是否存在注册表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

im trying to check if a registry key exists and no matter what i try i always get the error message "unable to open registry key for reading"

我使用的代码:

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

}

我在这里遗漏了什么?

推荐答案

您可能需要删除尾部斜杠.如果你使用它,它会为你指定的键寻找默认值,如果没有找到,就会报错.

You probably need to remove the trailing slash. If you use that, it will look for the default value for the key you have specified, and if it does not find it, will give that error.

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

Conversely, if you try to access a key as if it was a value by using no trailing slash, you will get the same error.

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

Some examples trying to access a key:

失败:

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

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

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

一些尝试访问值的示例:

Some examples trying to access a value:

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

Succeeds (output is 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);

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

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

这篇关于检查 WScript 是否存在注册表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆