简体   繁体   中英

How to create sub key in windows Registry Using Python winreg module?

I want to add subkey in windows registry using python winreg. See the below picture for better understanding.

在此处输入图像描述

Now, I'm using this code.

import winreg as rg
regkey = rg.OpenKey(rg.HKEY_CLASSES_ROOT, "Directory\shell", 0,rg.KEY_WRITE)
rg.SetValueEx(regkey, "IFO/dr",0,rg.REG_SZ, r"F:\IFO\dist\rename_folder.exe")

I just want to create the subkey and than want to add value.

To create a new sub_key, you need to use winreg.CreateKeyEx(key, sub_key, 0, access=KEY_CREATE_SUB_KEY) , where key is the root HKEY_ (Eg HKEY_LOCAL_MACHINE), and sub_key is the fullpath key you want to create.

import winreg

rootPath = r"Directory\shell"
keyPath = r"IFO\dr"

winreg.CreateKeyEx(winreg.HKEY_LOCAL_MACHINE, rootPath, 0, winreg.KEY_CREATE_SUB_KEY) # Open/Create the sub_key
winreg.SetValue(winreg.HKEY_LOCAL_MACHINE, rootPath+'\\'+keyPath, winreg.REG_SZ, r"F:\IFO\dist\rename_folder.txt") # Set its value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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