详细资料请参考:https://docs.python.org/2/library/_winreg.html
一、常用函数功能介绍
OpenKey() - 打开一个key
##################################################################################################################
_winreg.
OpenKey
(key, sub_key[, res[, sam]])
key is an already open key, or any one of the predefined HKEY_* constants. #注册表中有六大键根,HKEY_USERS,HKEY_CURRENT_USER,HKEY_CURRENT_CONFIG,HKEY_CLASSES_ROOT,HKEY_LOCAL_MACHINE,HKEY_DYN_DATA
sub_key is a string that identifies the sub_key to open. #需要操作的子键
res is a reserved integer, and must be zero. The default is zero. #必须为0
#sam默认为只读模式,常用有三个_winreg.KEY_ALL_ACCESS,_winreg.KEY_WRITE,_winreg.KEY_READ
sam is an integer that specifies an access mask that describes the desired security access for the key. Default is KEY_READ
. See Access Rights for other allowed values.
##################################################################################################################
CloseKey() – 关闭一个Key
import _winreg as wg key_test = wg.OpenKey(wg.HKEY_CURRENT_USER,r"SoftwareMicrosoftWindowsCurrentVersionExplorer") wg.CloseKey(key_test)
CreateKey() – 创建一个Key
DeleteKey() – 删除一个Key
import _winreg as wg key_test = wg.OpenKey(wg.HKEY_CURRENT_USER,r"SoftwareMicrosoftWindowsCurrentVersionExplorer") wg.CreateKey(key_test,'hester') wg.CloseKey(key_test)
继续创建子键
import _winreg as wg key_test = wg.OpenKey(wg.HKEY_CURRENT_USER,r"SoftwareMicrosoftWindowsCurrentVersionExplorerhester") wg.CreateKey(key_test,'sub_hester') wg.CloseKey(key_test)
创建键值数据项
import _winreg as wg key_test = wg.OpenKey(wg.HKEY_CURRENT_USER,r"SoftwareMicrosoftWindowsCurrentVersionExplorerhestersub_hester") wg.SetValueEx(key_test,'data','',wg.REG_SZ,'0')
wg.CloseKey(key_test)
修改键值数据项
##################################################################################################################
_winreg.
SetValueEx
(key, value_name, reserved, type, value)
key is an already open key, or one of the predefined HKEY_* constants.
value_name is a string that names the subkey with which the value is associated.
type is an integer that specifies the type of the data. See Value Types for the available types. #类型较多不一一列举,请参考https://docs.python.org/2/library/_winreg.html#value-types
reserved can be anything – zero is always passed to the API.
value is a string that specifies the new value.
##################################################################################################################
import _winreg as wg key_test = wg.OpenKey(wg.HKEY_CURRENT_USER,r"SoftwareMicrosoftWindowsCurrentVersionExplorerhestersub_hester") wg.SetValueEx(key_test,'data','',wg.REG_SZ,'1') wg.CloseKey(key_test)
获取键值数据项值
import _winreg as wg key_test = wg.OpenKey(wg.HKEY_CURRENT_USER,r"SoftwareMicrosoftWindowsCurrentVersionExplorerhestersub_hester") wg.QueryValueEx(key_test,'data') value,type = wg.QueryValueEx(key_test,'data')
删除键值数据项
import _winreg as wg key_test = wg.OpenKey(wg.HKEY_CURRENT_USER,r"SoftwareMicrosoftWindowsCurrentVersionExplorerhestersub_hester",0,wg.KEY_WRITE) wg.DeleteValue(key_test,'data') wg.CloseKey(key_test)
输入、输出值文件
LoadKey() – 从指定文件读入键信息
SaveKey() – 保存键到文件
刷新注册表
FlushKey() – 回写所有的键属性改变到注册表
链接到其他机器的注册表
ConnectRegistry() – 链接到其他机器的注册表