How to open a Windows Explorer in a user-created library?

443 views Asked by At

Ultimately, I want to be able to do this for a file dialog in C++ code, but being able to do it from a command line would be a good start...

For the 'standard' libraries - Documents, Music, Pictures, and Videos - I know there are special 'shell:' names that work (list here ).

So, for example,

explorer.exe shell:PicturesLibrary

will open an explorer directly in the Pictures library view.

But I've created a new library, say 'My Library', and I can't find any way to open an explorer directly in 'My Library'. Best I can do so far is to open 'shell:Libraries' and force the user to descend from there.

Anyone know?

1

There are 1 answers

2
Christopher Oicles On BEST ANSWER

This console app starts off by using SHGetKnownFolderPath to get the path to the user's libraries folder. Then it uses ShellExecute to "explore" the "My Library" library file within this directory.

The string given by SHGetKnownFolderPath is freed by CoTaskMemFree. Also, SHGetKnownFolderPath doesn't have an ANSI version, so I just made everything wide-character-explicit.

No error checking is included, so you might want to add that.

#include <Windows.h>
#include <Shellapi.h>
#include <Shlobj.h>

int main() {
    PWSTR libraries_path = NULL;
    SHGetKnownFolderPath(FOLDERID_Libraries, 0, NULL, &libraries_path);
    ShellExecuteW(NULL, L"explore", L"My Library.library-ms",NULL,libraries_path,SW_SHOW);
    CoTaskMemFree(libraries_path);
}

From the command-line, this does something similar:

"%appdata%\Microsoft\Windows\Libraries\My Library.library-ms"