How can I get the name for the user’s profile directory that is shown in Explorer?

Raymond Chen

A customer wanted to know how they could get the SHGet­Localized­Name function to work. Fortunately, they gave a specific definition of “work”.

We are populating a list of folders, and we want the names to be the same names that appear in Explorer. We are diong it by calling SHGet­Localized­Name, and if it succeeds, doing a Load­Library­Ex with LOAD_LIBRARY_AS_DATAFILE, and then a Load­String. (If the call to SHGet­Localized­Name fails, then we just use the raw file system name.) This works for the most part, but not for the user profile. How can we get the correct name for the user profile folder?

The user profile name is not a localized name, which is why SHGet­Localized­Name doesn’t give you any information about it. Instead, that name is dynamically generated based on the user whose profile you are looking at. (It’s not like there’s a giant usernames.dll which includes the name of every possible user on the planet, and SHGet­Localized­Name says “Oh, yeah, the user is Raymond Chen? That’s string number 31415 in usernames.dll.”)

The way to get the name that Explorer displays for an item is to ask the same data source that Explorer uses: the shell namespace.

And hey, how about that, I already wrote a sample program that shows how to do this, just in the context of a different question, so I’m going to incorporate the existing program by reference. Here you go.

0 comments

Discussion is closed.

Feedback usabilla icon