I'm working on a Python 3.7 script that eventually will be a CLI program like reg.exe is. I'm aiming to include the ability to add, delete and query keys and subkeys. At this point, I can create a new Key and iterate through all keys within the specific path however; once I try to write a value to the new key I made, I get a WinError 5 - Access denied.
Is there a way I can include in the script a way to have access to write to the registry?
I'm still a beginner with Python and programming, I've had a look at documents but I cant figure this one out.
Any help will be greatly appreciated. My code soo far:
import winreg
reg_connection = winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER)
reg_key = winreg.OpenKey(reg_connection, r"SOFTWARE\Microsoft\\")
winreg.CreateKey(reg_key, "New Key")
for key in range(3000):
try:
show_sub_keys = winreg.EnumKey(reg_key, key)
print(show_sub_keys)
except WindosError:
break
new_key_value = winreg.OpenKey(reg_connection, r"SOFTWARE\Microsoft\New Key")
winreg.SetValueEx(new_key_value, "New Value",0,winreg.REG_SZ, "This Value")
winreg.CloseKey(new_key_value)
ConnectRegistryjust to access the local registry. It maps a predefined key handle (i.e.HKEY_LOCAL_MACHINE,HKEY_USERS, orHKEY_PERFORMANCE_DATA) to an RPC handle for remote registry access.HKEY_CURRENT_USERis a predefined-handle that by default maps to a handle for the current user's registry key, "\Registry\User\<User SID String>". This is meaningless for remote access (no user profile is loaded), so it's not supported.