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

James Carroll mrmaple at gmail.com
Fri Mar 10 04:45:55 CET 2006


> >>> 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?

Thanks Tim,  That was wicked tricky.  This is valuable to know though,
I envy your insight.

I was giving my app's user a choice of user names just to try to
figure out the profile directory, but for this purpose I'm changing my
strategy to simply let them choose from a list of directories under
Documents and Settings instead.  Fewer hoops to jump through, and
they're very similar to the actual user names, and no ambiguity as to
if that user exists locally _and_ as member of a domain.  (at least
the ambiguity is pushed out to the user to ponder.)


> 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.
>

You da man, Tim.
-Jim


More information about the Python-win32 mailing list