简体   繁体   English

windows 注册表项中的斜杠

[英]Slashes in windows registry key

I'm trying to set full app path of test.exe in registry as name.我正在尝试将注册表中 test.exe 的完整应用程序路径设置为名称。 But it gives me wrong result.但它给了我错误的结果。

Expected Output:预计 Output:

在此处输入图像描述

Output: Output:

在此处输入图像描述 This is the code I'm using这是我正在使用的代码

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"

Is there any workaround for this?有什么解决方法吗?

This MSDN KB article says:这篇MSDN KB 文章说:

Due to the limitations of the RegWrite method of Windows Script Host (WSH) it is not possible to write a "\\" (backslash) in a key name or value name.由于 Windows 脚本宿主 (WSH) 的 RegWrite 方法的限制,无法在键名或值名中写入“\\”(反斜杠)。

This is by design and there is no workaround with WSH.这是设计使然,WSH 没有解决方法。 The article goes on to suggest using alternative scripting objects (WMI, RegObj.dll) to set such key and value names.本文继续建议使用替代脚本对象(WMI、RegObj.dll)来设置此类键名和值名。

Still using vbscript, try to create a .reg file and execute it.仍然使用vbscript,尝试创建一个.reg 文件并执行它。

Some code that does it in another path of the registry:在注册表的另一个路径中执行此操作的一些代码:

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

Just click OK when asked for elevation.当要求提升时,只需单击“确定”。 You may want to check the created file, so I am not deleting it in my code.您可能想要检查创建的文件,所以我不会在我的代码中删除它。

Another approach is to use WMI Registry provider另一种方法是使用 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"

Try using slash ( / ) as your file system path separator.尝试使用斜杠 ( / ) 作为文件系统路径分隔符。 WSH will correctly write A *nix style path to a registry value while a Windows style path will write as a sequence of sub keys. WSH 将正确地将 *nix 样式路径写入注册表值,而 Windows 样式路径将写入为子键序列。 However this depends on the software that is reading the registry value to grok the path correctly.但是,这取决于读取注册表值以正确查找路径的软件。 Many components of Windows will now accept either path separator. Windows 的许多组件现在都接受任一路径分隔符。 Give it a try.试一试。

You can use .ShellExecute to edit the registry with reg.exe .您可以使用.ShellExecute通过reg.exe编辑注册表。

ShellExecute guide ShellExecute 指南

Syntax:句法:

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

Key:钥匙:

Keyword关键词 Action行动
application应用 The file to execute (required)要执行的文件(必填)
parameters参数 Arguments for the executable Arguments 用于可执行文件
dir目录 Working directory工作目录
verb动词 The operation to execute (runas/open/edit/print)要执行的操作(runas/open/edit/print)
window window (1=normal, 0=hide, 2=Min, 3=max, 4=restore, 5=current, 7=min/inactive, 10=default) View mode application window (1=normal, 0=hide, 2=Min, 3=max, 4=restore, 5=current, 7=min/inactive, 10=default) 查看模式应用程序 window

Example:例子:

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

Where the Command is a path with a backslash to a .exe (like D:\Path with backslash\some program.exe ) that passed to your application as command line parameters (like start "" "C:\Path\your application.exe" "D:\Path with backslash\some program.exe" .其中 Command 是带有反斜杠的.exe路径(如D:\Path with backslash\some program.exe )作为命令行参数(如start "" "C:\Path\your application.exe" "D:\Path with backslash\some program.exe" .

I used MsgBox to ensure that is correct:我使用MsgBox来确保它是正确的:

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

You can also use CreateObject("WScript.Shell").Run as an alternative to run reg.exe and edit the registry.您还可以使用CreateObject("WScript.Shell").Run作为运行reg.exe和编辑注册表的替代方法。

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

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