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\\Sign 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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With