Get CSIDL_LOCAL_APPDATA path for any user on Windows
Asked Answered
S

1

4

Is there any Win32/MFC API to get the CSIDL_LOCAL_APPDATA for any user that I want (not only the currently logged on one)? Let's say I have a list of users in the form "domain\user" and I want to get a list of their paths - is that possible?

Smokeless answered 25/9, 2008 at 6:28 Comment(0)
V
6

You can get the SID for the user and then look it up under HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList and get the ProfileImagePath value.

Once you have this path, you can get CLSID_LOCAL_APPDATA for your user, convert the absolute path to a relative path to your profile and then append that relative path to the other user profile path.

However, keep in mind that this is relying on an undocumented registry key and can break in future versions of the OS. (Or, as Raymond Chan would say: "Now that you know how to do it, let me tell you why you shouldn't do it this way..." :-))

If you have a token representing the user, you can use the SHGetFolderPath or SHGetKnownFolderPath (on Vista and up). However, there are certain security restrictions and you should read up on MSDN for details.

SHGetFolderPath - http://msdn.microsoft.com/en-us/library/bb762181(VS.85).aspx SHGetKnownFolderPath - http://msdn.microsoft.com/en-us/library/bb762188(VS.85).aspx

Verse answered 25/9, 2008 at 6:35 Comment(6)
If there is another way, I'd gladly use it :)Smokeless
SHGetFolderPath() should do the trick - this app will be run as admin, so should be fine. Thanks.Smokeless
If I can't get a token for the SHGetFolderPath(), is there any other way I could accomplish this? Thanks.Smokeless
Nope, for security reasons you should not have access to other user's folders (including their paths) unless you have impersonation priviledge and valid user token (i.e. you've been granted rights by the user somehow to do stuff on their behalf)Verse
I guess this works until either the current user gets its LOCAL_APPDATA folder moved or the user you're interested in gets its LOCAL_APPDATA folder moved. One interesting testcase would be a PC upgraded from XP to Vista, with pre- and post-upgrade users.Schoolman
Yes, it will fail if a user moves her local appdata. It will work 99% of the times and will fail miserably for your boss'es boss :-)). That's why I mentioned that's not the right way to do it. SHGetFolderPath w/ impersonation is the right way, but you need a valid user token.Verse

© 2022 - 2024 — McMap. All rights reserved.