20

From Visual C++, how do I get the path to the current user's My Documents folder?

Edit:

I have this:

TCHAR my_documents[MAX_PATH];
HRESULT result = SHGetFolderPath(NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, my_documents);

However, result is coming back with a value of E_INVALIDARG. Any thoughts as to why this might be?

5 Answers 5

26

It depends on how old of a system you need compatibility with. For old systems, there's SHGetSpecialFolderPath. For somewhat newer systems, there's SHGetFolderPath. Starting with Vista, there's SHGetKnownFolderPath.

Here's some demo code that works, at least on my machine:

#include <windows.h>
#include <iostream>
#include <shlobj.h>

#pragma comment(lib, "shell32.lib")

int main() { 
    CHAR my_documents[MAX_PATH];
    HRESULT result = SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, my_documents);

    if (result != S_OK)
        std::cout << "Error: " << result << "\n";
    else
        std::cout << "Path: " << my_documents << "\n";
    return 0;
}
3
  • Thanks - I've tried SHGetFolderPath, and something's not quite working - would you mind having a look at my edit in the question?
    – Smashery
    Mar 10, 2010 at 6:28
  • This seems to be a python builtin now. stackoverflow.com/questions/6227590/…
    – totowtwo
    Jun 27, 2012 at 8:33
  • 2
    FYI, straight from ShlObj.h: #define CSIDL_MYDOCUMENTS CSIDL_PERSONAL // Personal was just a silly name for My Documents
    – Hybrid
    Nov 17, 2012 at 10:50
9

Use the SHGetFolderPath Windows API function and request CSIDL_MYDOCUMENTS.

2
3

Using Visual Studio 2017 with an MFC application under Windows 10 I am using the following code snippet with SHGetKnownFolderPath function to get the current user's Documents folder:

#include <string>     // include file for C++ native strings

//  . . .  other code.

PWSTR   ppszPath;    // variable to receive the path memory block pointer.

HRESULT hr = SHGetKnownFolderPath(FOLDERID_Documents, 0, NULL, &ppszPath);

std::wstring myPath;
if (SUCCEEDED(hr)) {
    myPath = ppszPath;      // make a local copy of the path
}

CoTaskMemFree(ppszPath);    // free up the path memory block

Note that the documentation has this to say about the path variable usage and the path returned:

ppszPath [out]

Type: PWSTR*

When this method returns, contains the address of a pointer to a null-terminated Unicode string that specifies the path of the known folder. The calling process is responsible for freeing this resource once it is no longer needed by calling CoTaskMemFree. The returned path does not include a trailing backslash. For example, "C:\Users" is returned rather than "C:\Users\".

For a list of the FOLDERID_ arguments possible see the MSDN article KNOWN_FOLDER_FLAG enumeration.

1

Note that CSIDL_PERSONAL will not return the desired folder if the user has changed the default save folder in the Win7 Documents library. To get the right folder, you need to use SHLoadLibraryFromKnownFolder to obtain the IShellLibrary for the Documents library, use IShellLibrary::GetDefaultSaveFolder to get the IShellItem for the library's default save folder, and finally use IShellItem::GetDisplayName to get the folder name.

1
std::string GetMyDocumentsFolderPath()
{
    wchar_t Folder[1024];
    HRESULT hr = SHGetFolderPathW(0, CSIDL_MYDOCUMENTS, 0, 0, Folder);
    if (SUCCEEDED(hr))
    {
        char str[1024];
        wcstombs(str, Folder, 1023);
        return str;
    }
    else return "";
}

cout<<GetMyDocumentsFolderPath()<<endl;

how about this solution? Its working fine for me.

1
  • It seems that you are not using the csidl argument. If I understood well, it should probably be put as the second argument of SHGetFolderPathW() instead of CSIDL_MYDOCUMENTS.
    – BrunoLevy
    Dec 16, 2015 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.