zoukankan      html  css  js  c++  java
  • Python模块_winreg操作注册表

    用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"SYSTEMCurrentControlSetServicesTcpipParametersInterfaces{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"SoftwareMicrosoftWindowsCurrentVersionExplorer")

    #删除键

    _winreg.DeleteKey(key, "Advanced")

    #删除键值

    _winreg.DeleteValue(key, "IconUnderline")

    #创建新的

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

        #给新创建的键添加键值

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

  • 相关阅读:
    Android性能优化--ANR
    Android性能优化--冷启动优化(Application)
    Android性能优化--UI卡顿
    Android性能优化--内存泄漏
    java反射
    数据结构之约瑟夫问题(循环链表)(C++版)
    数据结构之杨辉三角(队列实现)(C++版)
    vue组件间通信六种方式
    类型转换
    千分位
  • 原文地址:https://www.cnblogs.com/liuqing0328/p/5064027.html
Copyright © 2011-2022 走看看