Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get path to My Documents

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?

like image 287
Smashery Avatar asked Mar 10 '10 05:03

Smashery


People also ask

What is the file path to Documents in Windows 10?

If you cannot see it by this method, then, in the Run prompt, type %userprofile%\Documents and hit the enter key. It will open the Documents folder.

Where is My Documents folder on my phone?

By far the easiest way to find downloaded files on Android is to look in your app drawer for an app called Files or My Files. Google's Pixel phones come with a Files app, while Samsung phones come with an app called My Files.


2 Answers

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;
}
like image 191
Jerry Coffin Avatar answered Nov 06 '22 16:11

Jerry Coffin


Use the SHGetFolderPath Windows API function and request CSIDL_MYDOCUMENTS.

like image 41
James McNellis Avatar answered Nov 06 '22 17:11

James McNellis