在注册表值上保留双引号

Retain double quotes on registry value

我正在尝试向包含空格的注册表添加一个值,因此我想保留双引号。

我试过用'\"'转义双引号,但这没有效果

path_with_spaces = "C:\Users\me\i have space\app.exe"
argument_with_spaces = "Quick brown fox"

data = '"{}" -n "{}"'.format(path_with_spaces, argument_with_spaces)
command = 'REG ADD HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /f /v test /d "{}"'.format(data)
subprocess.call(command)

如果我打印 "command" 我得到:

REG ADD HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /f /v test /d ""C:\Users\me\i have space\app.exe" -n "Quick brown fox""

但是在使用 regedit 检查结果时没有看到双引号。即使我这样做:

data = '\"{}\" -n \"{}\"'.format(path_with_spaces, argument_with_spaces)

没有变化。使用 regedit 检查时的输出总是这样的:

C:\Users\me\i有space\app.exe -n Quick brown fox

我做错了什么?

当你在command变量中注入data变量时,它已经是一个字符串了。所以如果你想在 command 中保留 \,你必须在 data 声明中将它们加倍

我想你可以尝试这样的事情:

data = '\"{}\" -n \"{}\"'.format(path_with_spaces, argument_with_spaces)