Get path to My Documents

2020-02-01 00:45发布

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条回答
放荡不羁爱自由
2楼-- · 2020-02-01 00:50

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;
}
查看更多
【Aperson】
3楼-- · 2020-02-01 00:59

Use the SHGetFolderPath Windows API function and request CSIDL_MYDOCUMENTS.

查看更多
不美不萌又怎样
4楼-- · 2020-02-01 01:02

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.

查看更多
The star\"
5楼-- · 2020-02-01 01:09
std::string GetSystemFolderPaths(int csidl)
{
    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<<GetSystemFolderPaths(CSIDL_MYDOCUMENTS)<<endl;

how about this solution? Its working fine for me.

查看更多
闹够了就滚
6楼-- · 2020-02-01 01:15

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.

查看更多
登录 后发表回答