获取SYSTEM账户的环境变量
As you can see in Windows Control Panel 'System' applet there are two groups of environment variables: USER and SYSTEM. Here presents function for retrieve SYSTEM variable value.
1: # -*- coding: Windows-1251 -*-
2: '''
3: getenv_system.py
4:
5: Get SYSTEM environment value, as if running under Service or SYSTEM account
6:
7: Author: Denis Barmenkov <denis.barmenkov@gmail.com>
8:
9: Copyright: this code is free, but if you want to use it,
10: please keep this multiline comment along with function source.
11: Thank you.
12:
13: 2006-01-28 15:30
14: '''
15:
16: import os, win32api, win32con
17:
18: def getenv_system(varname, default=''):
19: v = default
20: try:
21: rkey = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, 'SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment')
22: try:
23: v = str(win32api.RegQueryValueEx(rkey, varname)[0])
24: v = win32api.ExpandEnvironmentStrings(v)
25: except:
26: pass
27: finally:
28: win32api.RegCloseKey(rkey)
29: return v
30:
31: print 'SYSTEM.TEMP => %s' % getenv_system('TEMP')
32: print 'USER.TEMP => %s' % os.getenv('TEMP')