获取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')

转载于:https://www.cnblogs.com/sgsoft/archive/2011/06/07/2074674.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值