我试图检查是否存在注册表项,无论我尝试什么,我总是收到错误消息“无法打开注册表项进行阅读”
我正在使用的代码:
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)
{
}
我在这里失踪了什么?
答案 0 :(得分: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);
成功(但由于默认值为空,因此会显示空结果):
var keyPath = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\";
var shell = new ActiveXObject("WScript.Shell");
var regValue = shell.RegRead(keyPath);
WScript.Echo("Value: " + regValue);
尝试访问值的一些示例:
成功(输出为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);
答案 1 :(得分:2)
您正试图打开HKLM蜂巢 可能WScript(或你启动它的用户)没有权限 你可以使用regedt32查看权限
答案 2 :(得分:0)
我得到的是,
如果未设置密钥的默认值,则会显示unable to open registry key '---' for reading.
现在,如果密钥具有默认值,并且您没有在密钥后附加\\
,那么您也会收到相同的错误。
因此,要获取默认值,您必须添加\\
,否则请在该键下添加完全关键字列表。例如“版本”,“位置”等。
答案 3 :(得分:0)
带有单斜杠和尾随斜线的vbscript最终为我的关键工作:
On Error Resume Next
Set WSHShell = CreateObject("WScript.Shell")
s = WSHShell.RegRead( "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\Word\" )
if Err.Number <> 0 then
MsgBox(Err.Description)
MsgBox("Office is not installed?" )
exit Function
Else
MsgBox("Office is installed")
exit Function
''wscript.quit
End If
MsgBox("xxxxxxxxxxxxxxxxx")