开发者

How to add a string registry entry that contains backslashes itself?

开发者 https://www.devze.com 2023-04-01 05:18 出处:网络
I want to add an entry in the registry at AppCompatFlagsRegistryKey = \"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers\\\" by using a Jscript. However the e

I want to add an entry in the registry at AppCompatFlagsRegistryKey = "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers\" by using a Jscript. However the entry that I want to add is a REG_S开发者_如何学JAVAZ which has a name with backslashes itself.

For example: name = "C:\Program Files\vendor\myPackage.exe" and the Data = "RUNASADMIN".

When I use: WshShell.RegWrite( AppCompatFlagsRegistryKey + name, value, type);

The result is that only the last part of the name (package.exe) is seen as leaf. I tried

name = "\" + "C:\Program Files\vendor\myPackage.exe" + "\"

but that does not help. Any suggestions what is the correct way to do this?


You must escape every \ in a literal string with \\ to avoid js from treating \? as an escape sequence;

name = "C:\\Program Files\\vendor\\myPackage.exe";

Edit:

Doesn't seem like you can do that with RegWrite, here is a way with WMI:

var AppCompatFlagsRegistryKey = "Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers";
var name = "C:\\Program Files\\vendor\\myPackage.exe";
var Data = "RUNASADMIN";
var result;
var objRegistry = GetObject("winmgmts://./root/default:StdRegProv");

try {
    result = objRegistry.SetStringValue(0x80000001 /*HKCU*/, AppCompatFlagsRegistryKey, name, Data);
} catch (e) {
    alert(e.message);
    result = 0;
}
alert("success: " + (result == 0));
0

精彩评论

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