views:

451

answers:

1

I'm looking for a good way to get the local application data folder for a specific user -- without having to enter the login details for that user.

SHGetFolderPath() can accept an access token for whatever user I want to get the local appdata folder for, but to get an access token, you have to provide the user's password. Also, according to the docs this isn't supported on <= Windows 2000.

The registry key HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folder contains the AppData value, which contains the location of the local appdata folder for the current user... so I could technically mount the HKCU hive for the specific user and access this value; however, Microsoft seems to strongly discourage the use of this key, and I've heard that it can occasionally be inaccurate as well.

Ironically, on Windows, an administrator account can easily access and modify all of the data in any user's appdata folder, if they just open up Explorer and browse to the correct location... but there doesn't seem to be an easy way to programmatically get the location of the appdata folder for a specific user.

So do I have any other options? Right now, accessing the registry (gasp) seems like the best option, but I'd like to use something official and not discouraged by Microsoft, if possible.

Any suggestions would be appreciated.

+2  A: 

There is no documented way to do this without the token AFAIK, but your best bet is to:

  1. Find the profile: HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList (%windir%\Profiles on Win9x)
  2. AdjustTokenPrivileges for SE_RESTORE_NAME
  3. RegLoadKey NTUSER.DAT (USER.DAT on 9x)
  4. Query ...\CurrentVersion\Explorer\User Shell Folders

Note that the correct registry key to check is User Shell Folders and not Shell Folders (And you might have to expand the path), see The long and sad story of the Shell Folders key for details

I also want to point out that if possible, you should store the data in programfiles or the common folders and then copy into the profile when your program runs for the first time for each user

Anders
Awesome. I did see the RegLoadKey() function, and considered using it... but at that time I wasn't sure it would be able to do what I wanted without being ridiculously slow and unreliable. However, your answer gave me the inspiration to actually try it out. It looks like I can make that work... it's not as slow as I thought it'd be, either.Hopefully this will work without a hitch for the client who wants this feature. They've got a lot of computers using AD/terminal services, and likely roaming user profiles, as well. RegConnectRegistry() might come in handy later on.
beepboopbopbop
If RegLoadKey speed is an issue, you could probably look in HKEY_USERS for the user's SID, that would save you the RegLoadKey call, but it only works for profiles that are already loaded.
Anders