tags:

views:

433

answers:

3

What is the best way to enumerate all of the user profiles on a computer?

I know how to get the currently logged in user profile, and I know how to get the "all user" profile. But I'd like to get a list of each and every profile on the computer.

+1  A: 

You could read in the contents of the directory one level above %ALLUSERSPROFILE%.

Then you would have the names of all accounts that are (or were, if not deleted completly) on the system. To verify that an acccount still exists, I would use LsaLookupNames2 . This function retrieves the SIDs that belong to account-names (deleted account == no SID).

HTH, flokra

flokra
+4  A: 

Before going the undocumented route like flokra suggests, I would try NetUserEnum() or NetQueryDisplayInformation()

If you want to go into undocumented land, HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList has a (incomplete) list of accounts (It's missing special accounts like ASPNET, HelpAssistant and SUPPORT_xxxx) It also has the path to the profile folder, which is a lot safer than using %ALLUSERSPROFILE%\..\ but why use it when there is GetProfilesDirectory()

Anders
Wow, I didn't know that (and actually search for this stuff in the msdn-library didn't bring this up). Now, it would be nice if your links to the documentation of these functions would work. Thanks.
flokra
Fixed the links, not sure why they broke in a previous edit
Anders
+1  A: 

I'd be very careful with that, in the presence of roaming profiles. When do you count a roaming profile to be on the computer? Also, do you have sufficient rights to access it? Even Local Admins don't have rights to network accounts, only network admins do IIRC.

MSalters