SHGetFolderPath() for a specific user

浪尽此生 提交于 2019-11-26 21:57:38

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/1059460/shgetfolderpath-for-a-specific-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!