开发者

check if a registry key exists with 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"

the code im using:

keyPath = "HKEY_LOCAL_MACHINE\\SOFTWARE\\BOS\\BOSaNOVA TCP/IP\\Set 1\\Cfg\\Sig开发者_如何学Gon On\\";

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

    shell = null;
}
catch(err)
{

}

what im missing here?


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:

Fails:

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

Succeeds (but gives empty result since Default value is empty):

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:

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

Fails (shouldn't use trailing slash when accessing a value):

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


you are trying to open HKLM hive and probably WScript (or user you start it) have no permissions for that
you can look on permissions with regedt32


What I got is ,

If the default value of the key is not been set then it will say unable to open registry key '---' for reading.

Now, if the key has a default value and you are not appending \\ after the key then also you will get the same error.

So, to get the default value you must add \\ , otherwise add the exact keyword listing under that key. e.g. "Version", "Location" etc.


vbscript with single back slash and trailing slash in the end for the key works for me:

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")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜