[python-win32] Python-win32 Digest, Vol 36, Issue 11

Tim Roberts timr at probo.com
Thu Mar 9 19:17:51 CET 2006


James Carroll wrote:

>>> I've got one more question for you if I haven't bugged you too much
>>> today.  I'm getting the list of usernames of active users, but the
>>> next thing I need is that user's APPDATA directory.
>>> 
>>> APPDATA=C:\Documents and Settings\username\Application Data
>>> 
>>> I'm tempted to get the APPDATA directory for the current user, and the
>>> username, and do a replace on the username directory, but If there's a
>>> more foolproof way of knowing what the APPDATA directory will be for
>>> that user, I'd sleep better.
>>    
>>

How tricky are you willing to get?

If you use win32net.NetUserGetInfo, you can fetch the "user_id" value
for the user name.  If you then search in the registry in
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProfileList, one of the
subkeys of that key will be a SID that ends in that user ID (for
example, for user ID 1234, you might find
S-1-5-21-3939404041-4142424343-4444454546-1234).  The "ProfileImagePath"
value within that key points to their profile directory.  You can guess
that "Application Data" will be inside there.

Or, for an even more precise answer, you can use that SID to look into
their user registry hive.  Using that exact example:
  HKEY_USERS\
    S-1-5-21-S-1-5-21-3939404041-4142424343-4444454546-1234\
      Software\
        Microsoft\
          Windows\
            CurrentVersion\
              Explorer\
                Shell\
The "AppData" value within that key is, really and truly, the exact path
to their "Application Data" folder.

Of course, you'll have to be an administrator to read that key.

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-win32 mailing list