Windows注册表键中的斜杠

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

enter image description here

输出:

输出:

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

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"

有没有解决这个问题的方法?


1
将路径设置为注册表值的数据,而不是其名称。 - selbie
那里也有太多\s。你似乎是从C语言的示例中复制的。就此而言,一个程序不应该做你试图做的事情。有更好的方法来请求提升,比如应用程序清单。 - Bob77
5个回答

4
这篇MSDN KB文章指出:

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

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

0

仍在使用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"""

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


0

尝试使用斜杠(/)作为文件系统路径分隔符。WSH将正确地将*nix风格的路径写入注册表值,而Windows风格的路径将被写入为子键序列。但是,这取决于读取注册表值的软件是否能够正确理解该路径。现在,Windows的许多组件都可以接受任何一种路径分隔符。试试看吧。


0

另一种方法是使用WMI注册表提供程序

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"

0

您可以使用.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

关键字:

关键词 操作
应用程序 要执行的文件(必需)
参数 可执行文件的参数
目录 工作目录
动作 要执行的操作(runas/open/edit/print)
窗口 (1=正常,0=隐藏,2=最小化,3=最大化,4=还原,5=当前,7=最小化/非活动,10=默认)查看应用程序窗口的视图模式

示例:

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

命令是一个带有反斜杠的路径到.exe(例如D:\带反斜杠的路径\一些程序.exe),作为命令行参数传递给您的应用程序(例如start "" "C:\Path\your application.exe" "D:\带反斜杠的路径\一些程序.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 并编辑注册表。

示例:

CreateObject("WScript.Shell").Run "reg.exe" & " delete " & """HKCU\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers""" & " /v " & """" & Command & """" & " /f ", 0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接