我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用_winreg.KEY_WRITE。
def save(self): if USE_WINDOWS: import _winreg try: key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, self.keyname, sam=_winreg.KEY_SET_VALUE | _winreg.KEY_WRITE) except: key = _winreg.CreateKey(_winreg.HKEY_CURRENT_USER, self.keyname) try: for k, v in self.values.iteritems(): _winreg.SetValueEx(key, str(k), 0, _winreg.REG_SZ, str(v)) finally: key.Close() else: d = os.path.dirname(self.filename) if not os.path.isdir(d): os.makedirs(d) f = open(self.filename, 'w') try: data = '\n'.join(["%s=%s" % (k,v) for k,v in self.values.iteritems()]) f.write(data) finally: f.close()
def onUninstall(): path = os.path.join(config.getUserDefaultConfigPath(), ".dbInstall") if os.path.exists(path): #This is an update. Bail. os.remove(path) return key = _winreg.OpenKeyEx(_winreg.HKEY_CURRENT_USER, "Environment", 0, _winreg.KEY_READ | _winreg.KEY_WRITE) try: value, typ = _winreg.QueryValueEx(key, "Path") except: return if value is None or value == "": return dir = os.path.dirname(__file__) if not isinstance(dir, unicode): dir = dir.decode(sys.getfilesystemencoding()) dir = dir.replace(addonHandler.DELETEDIR_SUFFIX, "") if value.find(dir) != -1: value = value.replace(";" + dir, "") value = value.replace(dir + ";", "") value = value.replace(dir, "") _winreg.SetValueEx(key, "Path", None, typ, value) sendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, u"Environment")
def windows_persistence(): import _winreg from _winreg import HKEY_CURRENT_USER as HKCU run_key = r'Software\Microsoft\Windows\CurrentVersion\Run' bin_path = sys.executable try: reg_key = _winreg.OpenKey(HKCU, run_key, 0, _winreg.KEY_WRITE) _winreg.SetValueEx(reg_key, 'br', 0, _winreg.REG_SZ, bin_path) _winreg.CloseKey(reg_key) return True, 'HKCU Run registry key applied' except WindowsError: return False, 'HKCU Run registry key failed'
def open_key(self, key, subkey): """ Opens a key """ try: return winreg.OpenKey(key, subkey, 0, winreg.KEY_WRITE) except: return None
def setAutoRunWithOsStart(key,app,start=True): import _winreg try: r = _winreg.OpenKey( _winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Run",0,_winreg.KEY_WRITE) if start: _winreg.SetValueEx(r,key,0,_winreg.REG_SZ,app) else: _winreg.DeleteValue(r,key) except: traceback.print_exc()
def onInstall(postPathBug = False): #Add ourself to the path, so that commands when spoken can be queried to us. #Only if we are truely installing though. addons = [] if not postPathBug: addons = addonHandler.getAvailableAddons() for addon in addons: if addon.name=="DictationBridge": #Hack to work around condition where #the uninstaller removes this addon from the path #After the installer for the updator ran. #We could use version specific directories, but wsr macros Does not #play nice with refreshing the path environment after path updates, # requiring a complete reboot of wsr, or commands spontaneously break cripticly. with open(os.path.join(config.getUserDefaultConfigPath(), ".dbInstall"), "w") as fi: fi.write("dbInstall") return key = _winreg.OpenKeyEx(_winreg.HKEY_CURRENT_USER, "Environment", 0, _winreg.KEY_READ | _winreg.KEY_WRITE) try: value, typ = _winreg.QueryValueEx(key, "Path") except: value, typ = None, _winreg.REG_EXPAND_SZ if value is None: value = "" dir = os.path.dirname(__file__) if not isinstance(dir, unicode): dir = dir.decode(sys.getfilesystemencoding()) dir = dir.replace(addonHandler.ADDON_PENDINGINSTALL_SUFFIX, "") log.info("addon directory: %r" % dir) log.info("current PATH: %r" % value) if value.lower().find(dir.lower()) == -1: if value != "": value += ";" value += dir log.info("new PATH: %r" % value) _winreg.SetValueEx(key, "Path", None, typ, value) sendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, u"Environment")
def set_mac_address(new_mac): """ set the device's MAC address """ # Locate adapter's registry and update network address (mac) reg_hdl = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) key = winreg.OpenKey(reg_hdl, WIN_REGISTRY_PATH) info = winreg.QueryInfoKey(key) # Find adapter key based on sub keys adapter_key = None adapter_path = None target_index = -1 for index in range(info[0]): subkey = winreg.EnumKey(key, index) path = WIN_REGISTRY_PATH + "\\" + subkey if subkey == 'Properties': break # Check for adapter match for appropriate interface new_key = winreg.OpenKey(reg_hdl, path) try: adapterDesc = winreg.QueryValueEx(new_key, "DriverDesc") if adapterDesc[0] == target_device: adapter_path = path target_index = index break else: winreg.CloseKey(new_key) except (WindowsError) as err: if err.errno == 2: # register value not found, ok to ignore pass else: raise err if adapter_path is None: print 'Device not found.' winreg.CloseKey(key) winreg.CloseKey(reg_hdl) return # Registry path found update mac addr adapter_key = winreg.OpenKey(reg_hdl, adapter_path, 0, winreg.KEY_WRITE) winreg.SetValueEx(adapter_key, "NetworkAddress", 0, winreg.REG_SZ, new_mac) winreg.CloseKey(adapter_key) winreg.CloseKey(key) winreg.CloseKey(reg_hdl) # Adapter must be restarted in order for change to take affect # print 'Now you should restart your netsh' restart_adapter(target_index) # regex to MAC address like 00-00-00-00-00-00 or 00:00:00:00:00:00 or # 000000000000