winregWindows registry accessWindows注册表访问


These functions expose the Windows registry API to Python. 这些函数向Python公开Windows注册表API。Instead of using an integer as the registry handle, a handle object is used to ensure that the handles are closed correctly, even if the programmer neglects to explicitly close them.句柄对象不是使用整数作为注册表句柄,而是用于确保句柄正确关闭,即使程序员忽略了显式关闭它们。

Changed in version 3.3:版本3.3中更改: Several functions in this module used to raise a WindowsError, which is now an alias of OSError.该模块中的几个函数曾引发WindowsError,该WindowsError现在是OSError的别名。

Functions功能

This module offers the following functions:该模块提供以下功能:

winreg.CloseKey(hkey)

Closes a previously opened registry key. 关闭以前打开的注册表项。The hkey argument specifies a previously opened key.hkey参数指定了以前打开的密钥。

Note

If hkey is not closed using this method (or via hkey.Close()), it is closed when the hkey object is destroyed by Python.如果hkey没有使用此方法(或通过hkey.Close())关闭,则当hkey对象被Python破坏时,它将关闭。

winreg.ConnectRegistry(computer_name, key)

Establishes a connection to a predefined registry handle on another computer, and returns a handle object.建立与另一台计算机上预定义注册表句柄的连接,并返回句柄对象

computer_name is the name of the remote computer, of the form r"\\computername". computer_name是远程计算机的名称,格式为r"\\computername"If None, the local computer is used.如果None,则使用本地计算机。

key is the predefined handle to connect to.是要连接到的预定义句柄。

The return value is the handle of the opened key. 返回值是打开的密钥的句柄。If the function fails, an OSError exception is raised.如果函数失败,则会引发OSError异常。

Raises an auditing event winreg.ConnectRegistry with arguments computer_name, key.引发审核事件winreg.ConnectRegistry,参数为computer_namekey

Changed in version 3.3:版本3.3中更改: See above.请参见上文

winreg.CreateKey(key, sub_key)

Creates or opens the specified key, returning a handle object.创建或打开指定的键,返回句柄对象

key is an already open key, or one of the predefined HKEY_* constants.是一个已打开的键,或者是预定义的HKEY_* constants之一。

sub_key is a string that names the key this method opens or creates.是一个字符串,用于命名此方法打开或创建的键。

If key is one of the predefined keys, sub_key may be None. 如果key是预定义的key之一,那么sub_key可以是NoneIn that case, the handle returned is the same key handle passed in to the function.在这种情况下,返回的句柄与传递给函数的键句柄相同。

If the key already exists, this function opens the existing key.如果钥匙已经存在,此功能将打开现有钥匙。

The return value is the handle of the opened key. 返回值是打开的密钥的句柄。If the function fails, an OSError exception is raised.如果函数失败,则会引发OSError异常。

Raises an auditing event winreg.CreateKey with arguments key, sub_key, access.引发具有参数keysub_keyaccess审核事件winreg.CreateKey

Raises an auditing event winreg.OpenKey/result with argument key.使用参数key引发审核事件winreg.OpenKey/result

Changed in version 3.3:版本3.3中更改: See above.请参见上文

winreg.CreateKeyEx(key, sub_key, reserved=0, access=KEY_WRITE)

Creates or opens the specified key, returning a handle object.创建或打开指定的键,返回句柄对象

key is an already open key, or one of the predefined HKEY_* constants.key是一个已打开的键,或者是预定义的HKEY_* constants之一。

sub_key is a string that names the key this method opens or creates.是一个字符串,用于命名此方法打开或创建的键。

reserved is a reserved integer, and must be zero. 是保留整数,必须为零。The default is zero.默认值为零。

access is an integer that specifies an access mask that describes the desired security access for the key. 是一个整数,用于指定描述密钥所需安全访问的访问掩码。Default is KEY_WRITE. 默认值为KEY_WRITESee Access Rights for other allowed values.有关其他允许的值,请参阅访问权限

If key is one of the predefined keys, sub_key may be None. 如果key是预定义的key之一,那么sub_key可以是NoneIn that case, the handle returned is the same key handle passed in to the function.在这种情况下,返回的句柄与传递给函数的键句柄相同。

If the key already exists, this function opens the existing key.如果钥匙已经存在,此功能将打开现有钥匙。

The return value is the handle of the opened key. 返回值是打开的密钥的句柄。If the function fails, an OSError exception is raised.如果函数失败,则会引发OSError异常。

Raises an auditing event winreg.CreateKey with arguments key, sub_key, access.引发具有参数keysub_keyaccess审核事件winreg.CreateKey

Raises an auditing event winreg.OpenKey/result with argument key.使用参数key引发审核事件winreg.OpenKey/result

New in version 3.2.版本3.2中新增。

Changed in version 3.3:版本3.3中更改: See above.请参见上文

winreg.DeleteKey(key, sub_key)

Deletes the specified key.删除指定的密钥。

key is an already open key, or one of the predefined HKEY_* constants.是一个已打开的键,或者是预定义的HKEY_* constants之一。

sub_key is a string that must be a subkey of the key identified by the key parameter. 是一个字符串,必须是由key参数标识的键的子键。This value must not be None, and the key may not have subkeys.该值不能为None,并且该键不能有子键。

This method can not delete keys with subkeys.此方法无法删除具有子键的键。

If the method succeeds, the entire key, including all of its values, is removed. 如果该方法成功,则删除整个键,包括其所有值。If the method fails, an OSError exception is raised.如果该方法失败,则会引发OSError异常。

Raises an auditing event winreg.DeleteKey with arguments key, sub_key, access.引发具有参数keysub_keyaccess审核事件winreg.DeleteKey

Changed in version 3.3:版本3.3中更改: See above.请参见上文

winreg.DeleteKeyEx(key, sub_key, access=KEY_WOW64_64KEY, reserved=0)

Deletes the specified key.删除指定的密钥。

Note

The DeleteKeyEx() function is implemented with the RegDeleteKeyEx Windows API function, which is specific to 64-bit versions of Windows. DeleteKeyEx()函数是使用RegDeleteKeyEx Windows API函数实现的,该函数特定于64位版本的Windows。See the RegDeleteKeyEx documentation.请参阅RegDeleteKeyEx文档

key is an already open key, or one of the predefined HKEY_* constants.是一个已打开的键,或者是预定义的HKEY_* constants之一。

sub_key is a string that must be a subkey of the key identified by the key parameter. 是一个字符串,必须是由key参数标识的键的子键。This value must not be None, and the key may not have subkeys.该值不能为None,并且该键不能有子键。

reserved is a reserved integer, and must be zero. The default is zero.是保留整数,必须为零。默认值为零。

access is an integer that specifies an access mask that describes the desired security access for the key. 是一个整数,用于指定描述密钥所需安全访问的访问掩码。Default is KEY_WOW64_64KEY. 默认值为KEY_WOW64_64KEYSee Access Rights for other allowed values.有关其他允许的值,请参阅访问权限

This method can not delete keys with subkeys.此方法无法删除具有子键的键。

If the method succeeds, the entire key, including all of its values, is removed. 如果该方法成功,则删除整个键,包括其所有值。If the method fails, an OSError exception is raised.如果该方法失败,则会引发OSError异常。

On unsupported Windows versions, NotImplementedError is raised.在不受支持的Windows版本上,会引发NotImplementedError

Raises an auditing event winreg.DeleteKey with arguments key, sub_key, access.引发具有参数keysub_keyaccess审核事件winreg.DeleteKey

New in version 3.2.版本3.2中新增。

Changed in version 3.3:版本3.3中更改: See above.请参见上文

winreg.DeleteValue(key, value)

Removes a named value from a registry key.从注册表项中删除命名值。

key is an already open key, or one of the predefined HKEY_* constants.是一个已打开的键,或者是预定义的HKEY_* constants之一。

value is a string that identifies the value to remove.是一个字符串,用于标识要删除的值。

Raises an auditing event winreg.DeleteValue with arguments key, value.引发带有参数keyvalue审核事件winreg.DeleteValue

winreg.EnumKey(key, index)

Enumerates subkeys of an open registry key, returning a string.枚举打开的注册表项的子项,返回一个字符串。

key is an already open key, or one of the predefined HKEY_* constants.是一个已打开的键,或者是预定义的HKEY_* constants之一。

index is an integer that identifies the index of the key to retrieve.是一个整数,用于标识要检索的键的索引。

The function retrieves the name of one subkey each time it is called. 每次调用该函数时,它都会检索一个子键的名称。It is typically called repeatedly until an OSError exception is raised, indicating, no more values are available.它通常被重复调用,直到引发OSError异常,表明没有更多的值可用。

Raises an auditing event winreg.EnumKey with arguments key, index.使用参数keyindex引发审核事件winreg.EnumKey

Changed in version 3.3:版本3.3中更改: See above.请参见上文

winreg.EnumValue(key, index)

Enumerates values of an open registry key, returning a tuple.枚举打开的注册表项的值,返回元组。

key is an already open key, or one of the predefined HKEY_* constants.是一个已打开的键,或者是预定义的HKEY_* constants之一。

index is an integer that identifies the index of the value to retrieve.是一个整数,用于标识要检索的值的索引。

The function retrieves the name of one subkey each time it is called. 每次调用该函数时,它都会检索一个子键的名称。It is typically called repeatedly, until an OSError exception is raised, indicating no more values.它通常被重复调用,直到引发OSError异常,指示不再有值。

The result is a tuple of 3 items:结果是一个由3项组成的元组:

Index指数

Meaning意思

0

A string that identifies the value name标识值名称的字符串

1

An object that holds the value data, and whose type depends on the underlying registry type保存值数据的对象,其类型取决于基础注册表类型

2

An integer that identifies the type of the value data (see table in docs for SetValueEx())一个标识值数据类型的整数(请参阅文档中的表中的SetValueEx()

Raises an auditing event winreg.EnumValue with arguments key, index.使用参数keyindex引发审核事件winreg.EnumValue

Changed in version 3.3:版本3.3中更改: See above.请参见上文

winreg.ExpandEnvironmentStrings(str)

Expands environment variable placeholders %NAME% in strings like REG_EXPAND_SZ:扩展REG_EXPAND_SZ等字符串中的环境变量占位符%NAME%

>>> ExpandEnvironmentStrings('%windir%')
'C:\\Windows'

Raises an auditing event winreg.ExpandEnvironmentStrings with argument str.使用参数str引发审核事件winreg.ExpandEnvironmentStrings

winreg.FlushKey(key)

Writes all the attributes of a key to the registry.将键的所有属性写入注册表。

key is an already open key, or one of the predefined HKEY_* constants.key是一个已打开的键,或者是预定义的HKEY_* constants之一。

It is not necessary to call FlushKey() to change a key. 不需要调用FlushKey()来更改键。Registry changes are flushed to disk by the registry using its lazy flusher. 注册表更改由注册表使用其惰性刷新器刷新到磁盘。Registry changes are also flushed to disk at system shutdown. 注册表更改也会在系统关闭时刷新到磁盘。Unlike CloseKey(), the FlushKey() method returns only when all the data has been written to the registry. CloseKey()不同,FlushKey()方法仅在所有数据都已写入注册表时返回。An application should only call FlushKey() if it requires absolute certainty that registry changes are on disk.只有当应用程序要求绝对确定注册表更改在磁盘上时,才应调用FlushKey()

Note

If you don’t know whether a FlushKey() call is required, it probably isn’t.如果您不知道是否需要FlushKey()调用,那么它可能不是。

winreg.LoadKey(key, sub_key, file_name)

Creates a subkey under the specified key and stores registration information from a specified file into that subkey.在指定的密钥下创建一个子密钥,并将指定文件中的注册信息存储到该子密钥中。

key is a handle returned by ConnectRegistry() or one of the constants HKEY_USERS or HKEY_LOCAL_MACHINE.keyConnectRegistry()返回的句柄,或者是常量HKEY_USERSHKEY_LOCAL_MACHINE之一。

sub_key is a string that identifies the subkey to load.是一个字符串,用于标识要加载的子键。

file_name is the name of the file to load registry data from. 是要从中加载注册表数据的文件的名称。This file must have been created with the SaveKey() function. 此文件必须是使用SaveKey()函数创建的。Under the file allocation table (FAT) file system, the filename may not have an extension.在文件分配表(FAT)文件系统下,文件名可能没有扩展名。

A call to LoadKey() fails if the calling process does not have the SE_RESTORE_PRIVILEGE privilege. 如果调用进程没有SE_RESTORE_PRIVILEGE权限,则对LoadKey()的调用将失败。Note that privileges are different from permissions – see the RegLoadKey documentation for more details.请注意,权限与权限不同,有关更多详细信息,请参阅RegLoadKey文档

If key is a handle returned by ConnectRegistry(), then the path specified in file_name is relative to the remote computer.如果keyConnectRegistry()返回的句柄,则file_name中指定的路径是相对于远程计算机的。

Raises an auditing event winreg.LoadKey with arguments key, sub_key, file_name.使用参数keysub_keyfile_name引发审核事件winreg.LoadKey

winreg.OpenKey(key, sub_key, reserved=0, access=KEY_READ)
winreg.OpenKeyEx(key, sub_key, reserved=0, access=KEY_READ)

Opens the specified key, returning a handle object.打开指定的键,返回句柄对象

key is an already open key, or one of the predefined HKEY_* constants.key是一个已打开的键,或者是预定义的HKEY_* constants之一。

sub_key is a string that identifies the sub_key to open.是一个字符串,用于标识要打开的子密钥。

reserved is a reserved integer, and must be zero. 是保留整数,必须为零。The default is zero.默认值为零。

access is an integer that specifies an access mask that describes the desired security access for the key. 是一个整数,用于指定描述密钥所需安全访问的访问掩码。Default is KEY_READ. 默认值为KEY_READSee Access Rights for other allowed values.有关其他允许的值,请参阅访问权限

The result is a new handle to the specified key.结果是指定键的新句柄。

If the function fails, OSError is raised.如果函数失败,则会引发OSError

Raises an auditing event winreg.OpenKey with arguments key, sub_key, access.使用参数keysub_keyaccess引发审核事件winreg.OpenKey/result

Raises an auditing event winreg.OpenKey/result with argument key.使用参数key引发审核事件winreg.OpenKey/result

Changed in version 3.2:版本3.2中更改: Allow the use of named arguments.允许使用命名参数。

Changed in version 3.3:版本3.3中更改: See above.请参见上文

winreg.QueryInfoKey(key)

Returns information about a key, as a tuple.以元组形式返回有关键的信息。

key is an already open key, or one of the predefined HKEY_* constants.key是一个已打开的键,或者是预定义的HKEY_* constants之一。

The result is a tuple of 3 items:结果是一个由3项组成的元组:

Index索引

Meaning意思

0

An integer giving the number of sub keys this key has.一个整数,给出该键的子键数。

1

An integer giving the number of values this key has.一个整数,给出该键的值数。

2

An integer giving when the key was last modified (if available) as 100’s of nanoseconds since Jan 1, 1601.一个整数,给出自1601年1月1日以来最后一次修改密钥的时间(如果可用)为100纳秒。

Raises an auditing event winreg.QueryInfoKey with argument key.使用参数key引发审核事件winreg.QueryInfoKey

winreg.QueryValue(key, sub_key)

Retrieves the unnamed value for a key, as a string.以字符串形式检索键的未命名值。

key is an already open key, or one of the predefined HKEY_* constants.是一个已打开的键,或者是预定义的HKEY_* constants之一。

sub_key is a string that holds the name of the subkey with which the value is associated. 是一个字符串,其中包含与该值关联的子键的名称。If this parameter is None or empty, the function retrieves the value set by the SetValue() method for the key identified by key.如果此参数为None或空,则函数将检索由键标识的键的SetValue()方法设置的值。

Values in the registry have name, type, and data components. 注册表中的值包含名称、类型和数据组件。This method retrieves the data for a key’s first value that has a NULL name. 此方法检索具有NULL名称的键的第一个值的数据。But the underlying API call doesn’t return the type, so always use QueryValueEx() if possible.但底层API调用不返回类型,因此如果可能,请始终使用QueryValueEx()

Raises an auditing event winreg.QueryValue with arguments key, sub_key, value_name.使用参数keysub_keyvalue_name引发审核事件winreg.QueryValue

winreg.QueryValueEx(key, value_name)

Retrieves the type and data for a specified value name associated with an open registry key.检索与打开的注册表项关联的指定值名称的类型和数据。

key is an already open key, or one of the predefined HKEY_* constants.是一个已打开的键,或者是预定义的HKEY_* constants之一。

value_name is a string indicating the value to query.是指示要查询的值的字符串。

The result is a tuple of 2 items:结果是一个由2项组成的元组:

Index索引

Meaning意思

0

The value of the registry item.注册表项的值。

1

An integer giving the registry type for this value (see table in docs for SetValueEx())一个整数,给出该值的注册表类型(请参阅文档中的表中的SetValueEx()

Raises an auditing event winreg.QueryValue with arguments key, sub_key, value_name.使用参数keysub_keyvalue_name引发审核事件winreg.QueryValue

winreg.SaveKey(key, file_name)

Saves the specified key, and all its subkeys to the specified file.将指定的键及其所有子键保存到指定的文件中。

key is an already open key, or one of the predefined HKEY_* constants.是一个已打开的键,或者是预定义的HKEY_* constants之一。

file_name is the name of the file to save registry data to. 是要将注册表数据保存到的文件的名称。This file cannot already exist. 此文件不能已存在。If this filename includes an extension, it cannot be used on file allocation table (FAT) file systems by the LoadKey() method.如果此文件名包含扩展名,则LoadKey()方法无法在文件分配表(FAT)文件系统上使用它。

If key represents a key on a remote computer, the path described by file_name is relative to the remote computer. 如果key表示远程计算机上的密钥,则file_name所描述的路径是相对于远程计算机的。The caller of this method must possess the SeBackupPrivilege security privilege. 此方法的调用方必须拥有SeBackupPrivilege安全权限。Note that privileges are different than permissions – see the Conflicts Between User Rights and Permissions documentation for more details.请注意,权限不同于权限。有关详细信息,请参阅“用户权限和权限之间的冲突”文档。

This function passes NULL for security_attributes to the API.此函数将security_attributesNULL传递给API。

Raises an auditing event winreg.SaveKey with arguments key, file_name.使用参数keyfile_name引发审核事件winreg.SaveKey

winreg.SetValue(key, sub_key, type, value)

Associates a value with a specified key.将值与指定的键相关联。

key is an already open key, or one of the predefined HKEY_* constants.是一个已打开的键,或者是预定义的HKEY_* constants之一。

sub_key is a string that names the subkey with which the value is associated.是一个字符串,用于命名与值关联的子键。

type is an integer that specifies the type of the data. 是一个整数,用于指定数据的类型。Currently this must be REG_SZ, meaning only strings are supported. 目前,它必须是REG_SZ,这意味着只支持字符串。Use the SetValueEx() function for support for other data types.使用SetValueEx()函数来支持其他数据类型。

value is a string that specifies the new value.是指定新值的字符串。

If the key specified by the sub_key parameter does not exist, the SetValue function creates it.如果sub_key参数指定的键不存在,则由SetValue函数创建它。

Value lengths are limited by available memory. 值长度受可用内存的限制。Long values (more than 2048 bytes) should be stored as files with the filenames stored in the configuration registry. This helps the registry perform efficiently.长值(超过2048字节)应存储为文件,文件名存储在配置注册表中。这有助于注册表有效地执行。

The key identified by the key parameter must have been opened with KEY_SET_VALUE access.key参数标识的密钥必须已通过KEY_SET_VALUE访问打开。

Raises an auditing event winreg.SetValue with arguments key, sub_key, type, value.使用参数keysub_keytypevalue引发审核事件winreg.SetValue

winreg.SetValueEx(key, value_name, reserved, type, value)

Stores data in the value field of an open registry key.将数据存储在打开的注册表项的值字段中。

key is an already open key, or one of the predefined HKEY_* constants.是一个已打开的键,或者是预定义的HKEY_* constants之一。

value_name is a string that names the subkey with which the value is associated.是一个字符串,用于命名与值关联的子键。

reserved can be anything – zero is always passed to the API.可以是任何值零总是传递给API。

type is an integer that specifies the type of the data. 是一个整数,用于指定数据的类型。See Value Types for the available types.有关可用类型,请参阅值类型

value is a string that specifies the new value.是指定新值的字符串。

This method can also set additional value and type information for the specified key. 此方法还可以为指定的键设置附加值和类型信息。The key identified by the key parameter must have been opened with KEY_SET_VALUE access.密钥参数标识的密钥必须已通过KEY_SET_VALUE访问打开。

To open the key, use the CreateKey() or OpenKey() methods.若要打开密钥,请使用CreateKey()OpenKey()方法。

Value lengths are limited by available memory. 值长度受可用内存的限制。Long values (more than 2048 bytes) should be stored as files with the filenames stored in the configuration registry. 长值(超过2048字节)应存储为文件,文件名存储在配置注册表中。This helps the registry perform efficiently.这有助于注册表有效地执行。

Raises an auditing event winreg.SetValue with arguments key, sub_key, type, value.使用参数keysub_keytypevalue引发审核事件winreg.SetValue

winreg.DisableReflectionKey(key)

Disables registry reflection for 32-bit processes running on a 64-bit operating system.禁用在64位操作系统上运行的32位进程的注册表反射。

key is an already open key, or one of the predefined HKEY_* constants.是一个已打开的键,或者是预定义的HKEY_* constants之一。

Will generally raise NotImplementedError if executed on a 32-bit operating system.如果在32位操作系统上执行,通常会引发NotImplementedError

If the key is not on the reflection list, the function succeeds but has no effect. 如果该键不在反射列表中,则该函数会成功,但没有效果。Disabling reflection for a key does not affect reflection of any subkeys.禁用键的反射不会影响任何子键的反射。

Raises an auditing event winreg.DisableReflectionKey with argument key.引发具有参数key审核事件winreg.DisableReflectionKey

winreg.EnableReflectionKey(key)

Restores registry reflection for the specified disabled key.还原指定的禁用项的注册表反射。

key is an already open key, or one of the predefined HKEY_* constants.是一个已打开的键,或者是预定义的HKEY_* constants之一。

Will generally raise NotImplementedError if executed on a 32-bit operating system.如果在32位操作系统上执行,通常会引发NotImplementedError

Restoring reflection for a key does not affect reflection of any subkeys.恢复键的反射不会影响任何子键的反射。

Raises an auditing event winreg.EnableReflectionKey with argument key.使用参数key引发审核事件winreg.EnableReflectionKey

winreg.QueryReflectionKey(key)

Determines the reflection state for the specified key.确定指定键的反射状态。

key is an already open key, or one of the predefined HKEY_* constants.是一个已打开的键,或者是预定义的HKEY_* constants之一。

Returns True if reflection is disabled.如果禁用反射,则返回True

Will generally raise NotImplementedError if executed on a 32-bit operating system.如果在32位操作系统上执行,通常会引发NotImplementedError

Raises an auditing event winreg.QueryReflectionKey with argument key.使用参数key引发审核事件winreg.QueryReflectionKey

Constants常量

The following constants are defined for use in many _winreg functions.以下常量是为在许多_winreg函数中使用而定义的。

HKEY_* Constants

winreg.HKEY_CLASSES_ROOT

Registry entries subordinate to this key define types (or classes) of documents and the properties associated with those types. 从属于该键的注册表项定义文档的类型(或类)以及与这些类型相关联的属性。Shell and COM applications use the information stored under this key.Shell和COM应用程序使用存储在此密钥下的信息。

winreg.HKEY_CURRENT_USER

Registry entries subordinate to this key define the preferences of the current user. 从属于该键的注册表项定义当前用户的首选项。These preferences include the settings of environment variables, data about program groups, colors, printers, network connections, and application preferences.这些首选项包括环境变量的设置、有关程序组的数据、颜色、打印机、网络连接和应用程序首选项。

winreg.HKEY_LOCAL_MACHINE

Registry entries subordinate to this key define the physical state of the computer, including data about the bus type, system memory, and installed hardware and software.从属于该键的注册表项定义计算机的物理状态,包括有关总线类型、系统内存以及已安装的硬件和软件的数据。

winreg.HKEY_USERS

Registry entries subordinate to this key define the default user configuration for new users on the local computer and the user configuration for the current user.从属于此项的注册表项定义本地计算机上新用户的默认用户配置和当前用户的用户配置。

winreg.HKEY_PERFORMANCE_DATA

Registry entries subordinate to this key allow you to access performance data. 从属于此注册表项的注册表项允许您访问性能数据。The data is not actually stored in the registry;数据实际上没有存储在注册表中;the registry functions cause the system to collect the data from its source.注册表函数使系统从其源收集数据。

winreg.HKEY_CURRENT_CONFIG

Contains information about the current hardware profile of the local computer system.包含有关本地计算机系统的当前硬件配置文件的信息。

winreg.HKEY_DYN_DATA

This key is not used in versions of Windows after 98.此密钥不用于98之后的Windows版本。

Access Rights访问权限

For more information, see Registry Key Security and Access.有关详细信息,请参阅注册表项安全和访问

winreg.KEY_ALL_ACCESS

Combines the STANDARD_RIGHTS_REQUIRED, KEY_QUERY_VALUE, KEY_SET_VALUE, KEY_CREATE_SUB_KEY, KEY_ENUMERATE_SUB_KEYS, KEY_NOTIFY, and KEY_CREATE_LINK access rights.

winreg.KEY_WRITE

Combines the STANDARD_RIGHTS_WRITE, KEY_SET_VALUE, and KEY_CREATE_SUB_KEY access rights.

winreg.KEY_READ

Combines the STANDARD_RIGHTS_READ, KEY_QUERY_VALUE, KEY_ENUMERATE_SUB_KEYS, and KEY_NOTIFY values.

winreg.KEY_EXECUTE

Equivalent to KEY_READ.

winreg.KEY_QUERY_VALUE

Required to query the values of a registry key.查询注册表项的值时必需。

winreg.KEY_SET_VALUE

Required to create, delete, or set a registry value.需要创建、删除或设置注册表值。

winreg.KEY_CREATE_SUB_KEY

Required to create a subkey of a registry key.需要创建注册表项的子项。

winreg.KEY_ENUMERATE_SUB_KEYS

Required to enumerate the subkeys of a registry key.需要枚举注册表项的子项。

winreg.KEY_NOTIFY

Required to request change notifications for a registry key or for subkeys of a registry key.需要为注册表项或注册表项的子项请求更改通知。

Reserved for system use.保留供系统使用。

64-bit Specific64位特定

For more information, see Accessing an Alternate Registry View.有关详细信息,请参阅访问备用注册表视图

winreg.KEY_WOW64_64KEY

Indicates that an application on 64-bit Windows should operate on the 64-bit registry view.指示64位Windows上的应用程序应在64位注册表视图上运行。

winreg.KEY_WOW64_32KEY

Indicates that an application on 64-bit Windows should operate on the 32-bit registry view.指示64位Windows上的应用程序应在32位注册表视图上运行。

Value Types值类型

For more information, see Registry Value Types.有关详细信息,请参阅注册表值类型

winreg.REG_BINARY

Binary data in any form.任何形式的二进制数据。

winreg.REG_DWORD

32-bit number.32位数字。

winreg.REG_DWORD_LITTLE_ENDIAN

A 32-bit number in little-endian format.小端序格式的32位数字。 Equivalent to REG_DWORD.相当于REG_DWORD

winreg.REG_DWORD_BIG_ENDIAN

A 32-bit number in big-endian format.big-endian格式的32位数字。

winreg.REG_EXPAND_SZ

Null-terminated string containing references to environment variables (%PATH%).Null结尾的字符串,包含对环境变量的引用(%PATH%)。

A Unicode symbolic link.Unicode符号链接。

winreg.REG_MULTI_SZ

A sequence of null-terminated strings, terminated by two null characters. null结尾的字符串序列,由两个null字符终止。(Python handles this termination automatically.)(Python会自动处理这种终止。)

winreg.REG_NONE

No defined value type.没有定义的值类型。

winreg.REG_QWORD

A 64-bit number.一个64位的数字。

New in version 3.6.版本3.6中新增。

winreg.REG_QWORD_LITTLE_ENDIAN

A 64-bit number in little-endian format. 一个采用小端序格式的64位数字。Equivalent to REG_QWORD.等效于REG_QWORD

New in version 3.6.版本3.6中新增。

winreg.REG_RESOURCE_LIST

A device-driver resource list.设备驱动程序资源列表。

winreg.REG_FULL_RESOURCE_DESCRIPTOR

A hardware setting.硬件设置。

winreg.REG_RESOURCE_REQUIREMENTS_LIST

A hardware resource list.硬件资源列表。

winreg.REG_SZ

A null-terminated string.null结尾的字符串。

Registry Handle Objects注册表句柄对象

This object wraps a Windows HKEY object, automatically closing it when the object is destroyed. 此对象包装一个WindowsHKEY对象,当该对象被销毁时自动将其关闭。To guarantee cleanup, you can call either the Close() method on the object, or the CloseKey() function.为了保证清理,可以对对象调用Close()方法,也可以调用CloseKey()函数。

All registry functions in this module return one of these objects.此模块中的所有注册表函数都返回其中一个对象。

All registry functions in this module which accept a handle object also accept an integer, however, use of the handle object is encouraged.该模块中所有接受句柄对象的注册表函数也接受整数,但鼓励使用句柄对象。

Handle objects provide semantics for __bool__() – thus句柄对象为__bool__()提供语义,因此

if handle:
print("Yes")

will print Yes if the handle is currently valid (has not been closed or detached).如果句柄当前有效(尚未关闭或分离),将打印Yes

The object also support comparison semantics, so handle objects will compare true if they both reference the same underlying Windows handle value.该对象还支持比较语义,因此如果句柄对象都引用相同的底层Windows句柄值,则句柄对象将比较为true

Handle objects can be converted to an integer (e.g., using the built-in int() function), in which case the underlying Windows handle value is returned. 句柄对象可以转换为整数(例如,使用内置的int()函数),在这种情况下,将返回底层的Windows句柄值。You can also use the Detach() method to return the integer handle, and also disconnect the Windows handle from the handle object.您还可以使用Detach()方法返回整数句柄,并断开Windows句柄与句柄对象的连接。

PyHKEY.Close()

Closes the underlying Windows handle.关闭基本的Windows句柄。

If the handle is already closed, no error is raised.如果句柄已关闭,则不会引发任何错误。

PyHKEY.Detach()

Detaches the Windows handle from the handle object.将Windows控制柄与控制柄对象分离。

The result is an integer that holds the value of the handle before it is detached. 结果是一个整数,它在分离句柄之前保持句柄的值。If the handle is already detached or closed, this will return zero.如果句柄已分离或关闭,则返回零。

After calling this function, the handle is effectively invalidated, but the handle is not closed. 调用此函数后,句柄实际上是无效的,但句柄并没有关闭。You would call this function when you need the underlying Win32 handle to exist beyond the lifetime of the handle object.当您需要基础Win32句柄存在于句柄对象的生存期之外时,您可以调用此函数。

Raises an auditing event winreg.PyHKEY.Detach with argument key.引发带有参数key审核事件winreg.PyHKEY.Detach

PyHKEY.__enter__()
PyHKEY.__exit__(*exc_info)

The HKEY object implements __enter__() and __exit__() and thus supports the context protocol for the with statement:HKEY对象实现了__enter__()__exit__(),因此支持with语句的上下文协议:

with OpenKey(HKEY_LOCAL_MACHINE, "foo") as key:
... # work with key

will automatically close key when control leaves the with block.当控件离开with块时,将自动关闭key