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

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.

Comments (6)
  1. skSdnW says:

    SHGetLocalizedName is too low level and was added to Vista because of the changes to SKU/languages but you had special folder names before this, XP had custom names for the document folders that included the username when you view them in My Computer IIRC.

    The correct way of thinking about this is that you want the same name as Explorer and this is the shell displayname and various settings controls what this name looks like (with or without file extensions etc.) and it would be possible for MS to add a setting so you could disable the fancy userprofile name.

  2. Nico says:

    Every time I read a question like this I wonder if the real problem is a lack of abstraction around the filesystem. Explorer already “wraps” the underlying files and directories, so maybe it would make sense for there to be a virtual layer which automatically handles translations like this which could be used by Explorer as well as third party programs. After all, while there are times a program does want to know the “real” file and folder names, don’t users usually want to see and interact with the localized and customized view?

    Is that crazy? (or does it already exist and nobody uses it?)

    1. Igor Tandetnik says:

      This layer of abstraction exists; it’s called “shell namespace”. It’s mentioned in the article you are commenting on.

    2. skSdnW says:

      It does exist; IShellFolder & IShellItem. The downside is that this is shell+COM and there are a lot of undocumented interfaces, try hosting those fancy control panel items that take over the whole view etc. (IExplorerBrowser solves some of these issues but removes too much control over the UI).

  3. I don’t understand.

    They used SHGet­Localized­Name on the user profile folder and it failed. Why the fail case (“use the raw file system name”) didn’t work for them? Let’s say a user is called Sara Kerrigan and her profile folder is QueenOfBlades.WINDOWS. Explorer shows the latter, not the former.

    1. skSdnW says:

      It depends on where you see the folder in the namespace tree. Sure, inside c:\users it is a normal folder like any other but the folder might appear in other places (FOLDERID_UserFiles etc.) and is not the exact same thing when you stay in the PIDL/IShellFolder domain but once you convert the PIDL to a path you end up in filesystem land and there is no way back.

Comments are closed.

Skip to main content