Obtain the environment variables of the system account

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.