我们从Python开源项目中,提取了以下11个代码示例,用于说明如何使用_winreg.REG_EXPAND_SZ。
def _get_reg_value(key, subkey, name): """Return registry value specified by key, subkey, and name. Environment variables in values of type REG_EXPAND_SZ are expanded if possible. """ key = _winreg.OpenKey(key, subkey) try: ret = _winreg.QueryValueEx(key, name) except WindowsError: return None else: key.Close() if ret[1] == _winreg.REG_EXPAND_SZ: return expandvars(ret[0]) else: return ret[0]
def get_shellfolders(branch=HKCU, key=SHELL_FOLDERS): """Return mapping of shell folder names (current user) to paths.""" key = _winreg.OpenKey(branch, key) folders = {} i = 0 while True: try: ret = _winreg.EnumValue(key, i) if ret[2] == _winreg.REG_EXPAND_SZ: folders[ret[0]] = expandvars(ret[1]) else: folders[ret[0]] = ret[1] except WindowsError: break i +=1 key.Close() return folders
def modify(): pythonpath = os.path.dirname(os.path.normpath(sys.executable)) scripts = os.path.join(pythonpath, "Scripts") appdata = os.environ["APPDATA"] if hasattr(site, "USER_SITE"): userpath = site.USER_SITE.replace(appdata, "%APPDATA%") userscripts = os.path.join(userpath, "Scripts") else: userscripts = None with _winreg.CreateKey(HKCU, ENV) as key: try: envpath = _winreg.QueryValueEx(key, PATH)[0] except WindowsError: envpath = DEFAULT paths = [envpath] for path in (pythonpath, scripts, userscripts): if path and path not in envpath and os.path.isdir(path): paths.append(path) envpath = os.pathsep.join(paths) _winreg.SetValueEx(key, PATH, 0, _winreg.REG_EXPAND_SZ, envpath) return paths, envpath
def setenv(self, name, value): # Note: for 'system' scope, you must run this as Administrator key = winreg.OpenKey(self.root, self.subkey, 0, winreg.KEY_ALL_ACCESS) winreg.SetValueEx(key, name, 0, winreg.REG_EXPAND_SZ, value) winreg.CloseKey(key) # For some strange reason, calling SendMessage from the current process # doesn't propagate environment changes at all. # TODO: handle CalledProcessError (for assert) check_call('''\ "%s" -c "import win32api, win32con; assert win32api.SendMessage(win32con.HWND_BROADCAST, win32con.WM_SETTINGCHANGE, 0, 'Environment')"''' % sys.executable)
def sz_expand(value, value_type): if value_type == reg.REG_EXPAND_SZ: return reg.ExpandEnvironmentStrings(value) else: return value
def SetRebootPendingTime(reset=False): if reset: now = "None" else: now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") with _winreg.CreateKeyEx(_winreg.HKEY_LOCAL_MACHINE, R"SOFTWARE\Wpkg-GP-Client", 0, _winreg.KEY_ALL_ACCESS | _winreg.KEY_WOW64_64KEY) as key: _winreg.SetValueEx(key, "RebootPending", 0, _winreg.REG_EXPAND_SZ, now)
def setEnvironment(scope, name, value): assert scope in ('user', 'system') #INFO_MSG('set environment: name=%s, value=%s' % (name, value)) if platform.system() == 'Windows': root, subkey = getWindowsEnvironmentKey(scope) # Note: for 'system' scope, you must run this as Administrator key = winreg.OpenKey(root, subkey, 0, winreg.KEY_ALL_ACCESS) winreg.SetValueEx(key, name, 0, winreg.REG_EXPAND_SZ, value) winreg.CloseKey(key) else: if name.lower() == 'uid': uid, username = value if uid != str(os.geteuid()): ret, cret = syscommand('bash -c \'usermod -d /home/%s/ -u %s %s\'' % (pwd.getpwnam(username).pw_dir, uid, username), True) INFO_MSG(ret) INFO_MSG(cret) return userhome = "~" if len(os_user_name) > 0: userhome = pwd.getpwnam(os_user_name).pw_dir f = open('%s/.bashrc' % userhome, 'a') f.write("export %s=%s\n\n" % (name, value)) f.close() if os.geteuid() > 0: syscommand('bash -c \'source %s/.bashrc\'' % userhome, False)
def Reg2Py(data, size, data_type): if data_type == _winreg.REG_DWORD: if size == 0: return 0 return ctypes.cast(data, ctypes.POINTER(ctypes.c_int)).contents.value elif data_type == _winreg.REG_SZ or data_type == _winreg.REG_EXPAND_SZ: return ctypes.wstring_at(data, size // 2).rstrip(u"\x00") elif data_type == _winreg.REG_MULTI_SZ: return ctypes.wstring_at(data, size // 2).rstrip(u"\x00").split(u"\x00") else: if size == 0: return None return ctypes.string_at(data, size)
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")