我尝试基于存储在JSON对象中的值来设置注册表项。在HKEY_LOCAL_USER中创建密钥没有任何问题。我试图在HKEY_LOCAL_MACHINE中创建的任何内容都不会被创建。在具有和不具有管理员权限的情况下运行命令提示符时,无法创建密钥。运行Python 3.8。
from winreg import *
import ctypes, json, sys, time
#Determine the version of PDM and the branch location.
settingsData = 'EPDM_Vault_settings.txt'
solidworksVersion = 'TEST'
branch = '02'
pdmConfig = solidworksVersion + '_' + branch #Used to select settings from EPDM_Vault_settings.txt
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
def set_reg(key, subKey, name, regType, value):
try:
openKey=CreateKey(key, subKey)
SetValueEx(openKey, name, 0, regType, value)
CloseKey(openKey)
return False
except WindowsError:
print("\n**** error creating key for " + str(key) + ' > ' + str(subKey) + ' > ' + str(name) + ' > ' + str(regType) + ' > ' + str(value) + ' : ' + str(WindowsError))
return True
def get_key(v):
switcher={
"HKEY_CURRENT_USER": HKEY_CURRENT_USER,
"HKEY_LOCAL_MACHINE": HKEY_LOCAL_MACHINE
}
return switcher.get(v, False)
def get_regType(v):
switcher={
'REG_DWORD': 4,
'REG_SZ': 1,
}
return switcher.get(v, False)
def set_dataType(v, t):
if t == 'REG_DWORD':
return int(v)
elif t == 'REG_SZ':
return str(v)
else:
return False
def configure_settings():
with open(settingsData) as json_file:
data = json.load(json_file)
error = False
for p in data[pdmConfig]:
key = get_key(p['key'])
subKeys = p['subKeys']
for e in subKeys:
subKey = e['subKey']
settings = e['settings']
for i in settings:
name = i[0]
regType = get_regType(i[1])
value = set_dataType(i[2], i[1])
print(key, subKey, name, regType, value)
if key is not False and subKey is not False and value is not False:
error = set_reg(key, subKey, name, regType, value)
print(error)
else:
error = True
print('False detected at ' + str(key) + ' > ' + str(subKey) + ' > ' + str(name) + ' > ' + str(regType) + ' > ' + str(value))
if error == False:
print('\n****Success creating registry keys for SolidWorks EPDM configuration ' + pdmConfig + '\n')
else:
print('\n****WARNING: Registry key creation has failed. Check for typos and errors for configuration ' + pdmConfig + ' in ' + settingsData + '.\n')
print('****IMPORTANT: SolidWorks EPDM will not function propertly until the registry keys are created.\n')
if is_admin():
# Code of your program here
print('already running as admin')
configure_settings()
else:
# Re-run the program with admin rights
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
print('switched to running as admin')
configure_settings()JSON如下:
{
"TEST_02":[
{
"key": "HKEY_CURRENT_USER",
"subKeys": [
{
"subKey": "Software\\SolidWorks\\Solidworks 2017\\TEST FOLDER1",
"settings": [
["", "REG_SZ", "{ED78333F-D5DB-11D4-BD5A-00C04F019808}"]
]
},
{
"subKey": "Software\\SolidWorks\\Solidworks 2017\\TEST FOLDER2",
"settings": [
["the reg_sz", "REG_SZ", "some\\folder\\path"],
["the dword", "REG_DWORD", "123"]
]
}
]
},
{
"key": "HKEY_LOCAL_MACHINE",
"subKeys": [
{
"subKey": "SOFTWARE\\SolidWorks\\SOLIDWORKS 2017\\test folder",
"settings": [
["the dword", "REG_DWORD", "123"]
]
}
]
}
]
}发布于 2020-02-22 06:34:43
这看起来很有希望。
注册表有两个视图。有32位注册表视图和64位注册表视图。默认情况下,在大多数情况下,32位应用程序只能看到32位注册表视图,而64位应用程序只能看到64位注册表视图。
winreg.OpenKey throws filenotfound error for existing registry keys
https://stackoverflow.com/questions/60306606
复制相似问题