码迷,mamicode.com
首页 > 编程语言 > 详细

转 Python模块——_winreg操作注册表

时间:2016-12-22 14:53:51      阅读:480      评论:0      收藏:0      [点我收藏+]

标签:controls   参考   machine   如何   10.10   images   odi   user   5.0   

Python模块——_winreg操作注册表

 (2010-01-22 13:47:01)
标签: 

python

 

_winreg

 

注册表

 

删除键

 

name

 

默认网关

 

utf-8

 

it

 
分类: Python

    用python操作修改windows注册表,显然要比用C或者C++简单。

    主要参考资料:官方文档:http://docs.python.org/library/_winreg.html

通过python操作注册表主要有两种方式,一种是通过python的内置模块 _winreg,另一种方式就是Win32 Extension For Python的win32api模块。这里主要简单看看用内置模块 _winreg如何操作注册表。

 

1.读取

读取用的方法是OpenKey方法:打开特定的key

_winreg.OpenKey(key,sub_key,res=0,sam=KEY_READ)

例子:此例子是显示了本机网络配置的一些注册表项

#!/usr/bin/env python

#coding=utf-8

 

import _winreg

 

key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,                    r"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\{0E184877-D910-4877-B 4C2-04F487B6DBB7}")

#获取该键的所有键值,遍历枚举

try:

    i=0

    while 1:

        #EnumValue方法用来枚举键值,EnumKey用来枚举子键

        name,value,type = _winreg.EnumValue(key,i)

        print repr(name),value,type

        i+=1

except WindowsError:

    print      

#假如知道键名,也可以直接取值

value,type = _winreg.QueryValueEx(key,"DhcpDefaultGateway")

print "默认网关地址----",value,type

 

运行的结果如下:

‘UseZeroBroadcast‘ 0 4

‘EnableDeadGWDetect‘ 1 4

‘EnableDHCP‘ 1 4

‘IPAddress‘ [u‘0.0.0.0‘] 7

‘SubnetMask‘ [u‘0.0.0.0‘] 7

‘DefaultGateway‘ [] 7

‘DefaultGatewayMetric‘ [] 7

‘NameServer‘ 10.0.0.10 1

‘Domain‘  1

‘RegistrationEnabled‘ 1 4

‘RegisterAdapterName‘ 0 4

‘TCPAllowedPorts‘ [u‘0‘] 7

‘UDPAllowedPorts‘ [u‘0‘] 7

‘RawIPAllowedProtocols‘ [u‘0‘] 7

‘NTEContextList‘ [u‘0x00000004‘] 7

‘DhcpClassIdBin‘ None 3

‘DhcpServer‘ 10.104.4.1 1

‘Lease‘ 907200 4

‘LeaseObtainedTime‘ 1264122113 4

‘T1‘ 1264575713 4

‘T2‘ 1264915913 4

‘LeaseTerminatesTime‘ 1265029313 4

‘IPAutoconfigurationAddress‘ 0.0.0.0 1

‘IPAutoconfigurationMask‘ 255.255.0.0 1

‘IPAutoconfigurationSeed‘ 0 4

‘AddressType‘ 0 4

‘IsServerNapAware‘ 0 4

‘DhcpIPAddress‘ 10.104.5.15 1

‘DhcpSubnetMask‘ 255.255.254.0 1

‘DhcpRetryTime‘ 453598 4

‘DhcpRetryStatus‘ 0 4

‘DhcpNameServer‘ 10.0.0.10 1

‘DhcpDefaultGateway‘ [u‘10.104.4.1‘] 7

‘DhcpSubnetMaskOpt‘ [u‘255.255.254.0‘] 7

 

默认网关地址---- [u‘10.104.4.1‘] 7

 

2.创建 修改注册表

  创建key:_winreg.CreateKey(key,sub_key)

  删除key: _winreg.DeleteKey(key,sub_key)

  删除键值: _winreg.DeleteValue(key,value)

  给新建的key赋值: _winreg.SetValue(key,sub_key,type,value)

例子:

#!/usr/bin/env python

#coding=utf-8

import _winreg

 

key=_winreg.OpenKey(_winreg.HKEY_CURRENT_USER,r"Software\Microsoft\Windows\CurrentVersion\Explorer")

#删除键

_winreg.DeleteKey(key, "Advanced")

#删除键值

_winreg.DeleteValue(key, "IconUnderline")

#创建新的

newKey = _winreg.CreateKey(key,"MyNewkey")

    #给新创建的键添加键值

_winreg.SetValue(newKey,"ValueName",0,"ValueContent")

转 Python模块——_winreg操作注册表

标签:controls   参考   machine   如何   10.10   images   odi   user   5.0   

原文地址:http://www.cnblogs.com/chengxuyuan326260/p/6210741.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!