我正在尝试使用winreg将以下注册表调整转换为python:
REGEDIT4
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\IniFileMapping\Autorun.inf]
@="@SYS:Does_Not_Exist"
痛苦在'@',我似乎无法复制。查看C#中的示例,他们使用例如要输入@的空字符串''。如果我手动导入上面的内容并使用winreg的EnumValue(),则此条目也会显示为''。但我似乎无法在python winreg中做类似的事情,到目前为止我找不到任何解决方法。
显示问题的代码:
from winreg import *
import os
import platform
import sys, time
import win32api as wa, win32con as wc, win32service as ws
def registrySetKey(hive, regpath, key, type, value):
try:
reg = OpenKey(hive, regpath, 0, KEY_ALL_ACCESS)
except EnvironmentError:
try:
reg = CreateKey(hive, regpath, 0, KEY_ALL_ACCESS)
SetValueEx(reg, key, None, type, value)
CloseKey(reg)
except:
print("*** Unable to register path %s, key %s!" % (regpath, key))
return
print("--- Python", version, "is now registered!")
return
try:
if (QueryValue(reg, key) == value):
return
except:
SetValueEx(reg, key, None, type, value)
CloseKey(reg)
reg = CreateKey(HKEY_LOCAL_MACHINE, r'Software\Microsoft\Windows NT\CurrentVersion\IniFileMapping\Autorun.inf')
# This does not work
registrySetKey(HKEY_LOCAL_MACHINE,
r'Software\Microsoft\Windows NT\CurrentVersion\IniFileMapping\Autorun.inf',
'', REG_SZ, '@SYS:Does_Not_Exist')
手动导入后,条目名称为(默认),使用该名称也不起作用。
此致 斯文
答案 0 :(得分:0)
您正在使用的功能适用于您的通话,但您需要在值当前不匹配时添加以下内容,即它当前没有设置任何内容:
else:
SetValueEx(reg, key, None, type, value)
所以完整的功能如下:
from winreg import *
import os
import platform
import sys, time
import win32api as wa, win32con as wc, win32service as ws
def registrySetKey(hive, regpath, key, type, value):
try:
reg = OpenKey(hive, regpath, 0, KEY_ALL_ACCESS)
except EnvironmentError:
try:
reg = CreateKey(hive, regpath, 0, KEY_ALL_ACCESS)
SetValueEx(reg, key, None, type, value)
CloseKey(reg)
except:
print("*** Unable to register path %s, key %s!" % (regpath, key))
return
print("--- Python", version, "is now registered!")
return
try:
if (QueryValue(reg, key) == value):
return
else:
SetValueEx(reg, key, None, type, value) # added
except:
SetValueEx(reg, key, None, type, value)
CloseKey(reg)
reg = CreateKey(HKEY_LOCAL_MACHINE, r'Software\Microsoft\Windows NT\CurrentVersion\IniFileMapping\Autorun.inf')
registrySetKey(HKEY_LOCAL_MACHINE,
r'Software\Microsoft\Windows NT\CurrentVersion\IniFileMapping\Autorun.inf',
'', REG_SZ, '@SYS:Does_Not_Exist')
根据您的Windows版本,执行此操作可能会修改以下密钥:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\IniFileMapping\Autorun.inf