python _winreg模塊


詳細資料請參考:https://docs.python.org/2/library/_winreg.html

一、常用函數功能介紹

OpenKey() - 打開一個key

##################################################################################################################

_winreg.OpenKey(keysub_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"Software\Microsoft\Windows\CurrentVersion\Explorer")
wg.CloseKey(key_test)

 

CreateKey() – 創建一個Key
DeleteKey() – 刪除一個Key

import _winreg as wg
key_test = wg.OpenKey(wg.HKEY_CURRENT_USER,r"Software\Microsoft\Windows\CurrentVersion\Explorer")
wg.CreateKey(key_test,'hester')
wg.CloseKey(key_test)

 

繼續創建子鍵

import _winreg as wg
key_test = wg.OpenKey(wg.HKEY_CURRENT_USER,r"Software\Microsoft\Windows\CurrentVersion\Explorer\hester")
wg.CreateKey(key_test,'sub_hester')
wg.CloseKey(key_test)

 

創建鍵值數據項

import _winreg as wg
key_test = wg.OpenKey(wg.HKEY_CURRENT_USER,r"Software\Microsoft\Windows\CurrentVersion\Explorer\hester\sub_hester")
wg.SetValueEx(key_test,'data','',wg.REG_SZ,'0') 
wg.CloseKey(key_test)

 

修改鍵值數據項

##################################################################################################################

_winreg.SetValueEx(keyvalue_namereservedtypevalue)

  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"Software\Microsoft\Windows\CurrentVersion\Explorer\hester\sub_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"Software\Microsoft\Windows\CurrentVersion\Explorer\hester\sub_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"Software\Microsoft\Windows\CurrentVersion\Explorer\hester\sub_hester",0,wg.KEY_WRITE)
wg.DeleteValue(key_test,'data')
wg.CloseKey(key_test)

 

輸入、輸出值文件

LoadKey() – 從指定文件讀入鍵信息

SaveKey() – 保存鍵到文件

 

刷新注冊表

FlushKey() – 回寫所有的鍵屬性改變到注冊表

 

鏈接到其他機器的注冊表

ConnectRegistry() – 鏈接到其他機器的注冊表


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM