繁体   English   中英

windows 注册表项中的斜杠

[英]Slashes in windows registry key

我正在尝试将注册表中 test.exe 的完整应用程序路径设置为名称。 但它给了我错误的结果。

预计 Output:

在此处输入图像描述

Output:

在此处输入图像描述 这是我正在使用的代码

Dim WshShell, bKey
Set WshShell = WScript.CreateObject("WScript.Shell")


WshShell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers\D:\\\Program Files\\\test.exe", "RUNASADMIN", "REG_SZ"

有什么解决方法吗?

这篇MSDN KB 文章说:

由于 Windows 脚本宿主 (WSH) 的 RegWrite 方法的限制,无法在键名或值名中写入“\\”(反斜杠)。

这是设计使然,WSH 没有解决方法。 本文继续建议使用替代脚本对象(WMI、RegObj.dll)来设置此类键名和值名。

仍然使用vbscript,尝试创建一个.reg 文件并执行它。

在注册表的另一个路径中执行此操作的一些代码:

Set fs = CreateObject("Scripting.FileSystemObject")
Set shell = CreateObject("WScript.Shell")

'create .reg file:
Set reg_file = fs.CreateTextFile("slash.reg")
reg_file.WriteLine "Windows Registry Editor Version 5.00"
reg_file.WriteLine "[HKEY_CLASSES_ROOT\.txt]" 'put your path here

key_name = "D:\\Program Files\\test.exe" 'must be escaped inside the .reg file, so they enter as single slash in the registry
key_value = "RUNASADMIN"

reg_file.WriteLine """" & key_name & """=""" & key_value & """" 'escaping quotes inside vbscript string literal
reg_file.Close

'run it automatically to insert data (may ask for elevated privileges):
path = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
shell.run "regedit.exe /s """ & path & "slash.reg"""

当要求提升时,只需单击“确定”。 您可能想要检查创建的文件,所以我不会在我的代码中删除它。

另一种方法是使用 WMI Registry provider

Const REG_HIVE_HKLM = &H80000002
Const ROOT = "Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"

Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")
Set SWbemServicesReg = SWBemlocator.ConnectServer(".", "root\DEFAULT","","")
Set reg = SWbemServicesReg.Get("StdRegProv")

' if key is missing - create first, otherwise value won't be saved (without exception)
reg.CreateKey REG_HIVE_HKLM, ROOT

' set value
reg.SetStringValue REG_HIVE_HKLM, ROOT, "D:\Program Files\test.exe", "RUNASADMIN"

尝试使用斜杠 ( / ) 作为文件系统路径分隔符。 WSH 将正确地将 *nix 样式路径写入注册表值,而 Windows 样式路径将写入为子键序列。 但是,这取决于读取注册表值以正确查找路径的软件。 Windows 的许多组件现在都接受任一路径分隔符。 试一试。

您可以使用.ShellExecute通过reg.exe编辑注册表。

ShellExecute 指南

句法:

CreateObject("Shell.Application").ShellExecute "application", "parameters", "dir", "verb", window
CreateObject("Shell.Application").ShellExecute 'some program.exe', '"some parameters with spaces"', , "runas", 1

钥匙:

关键词 行动
应用 要执行的文件(必填)
参数 Arguments 用于可执行文件
目录 工作目录
动词 要执行的操作(runas/open/edit/print)
window (1=normal, 0=hide, 2=Min, 3=max, 4=restore, 5=current, 7=min/inactive, 10=default) 查看模式应用程序 window

例子:

CreateObject("Shell.Application").ShellExecute "reg.exe", "add " & """HKCU\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers""" & " /v " & """" & Command & """" & " /t REG_SZ /d " & """~ DISABLEDXMAXIMIZEDWINDOWEDMODE RUNASADMIN HIGHDPIAWARE""" & " /f ", , , 0

其中 Command 是带有反斜杠的.exe路径(如D:\Path with backslash\some program.exe )作为命令行参数(如start "" "C:\Path\your application.exe" "D:\Path with backslash\some program.exe" .

我使用MsgBox来确保它是正确的:

MsgBox "add " & """HKCU\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers""" & " /v " & """" & Command & """" & " /t REG_SZ /d " & """~ DISABLEDXMAXIMIZEDWINDOWEDMODE RUNASADMIN HIGHDPIAWARE""" & " /f "

您还可以使用CreateObject("WScript.Shell").Run作为运行reg.exe和编辑注册表的替代方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM