C++ 获取我的文档的路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2414828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:25:06  来源:igfitidea点击:

Get path to My Documents

c++visual-c++mfc

提问by Smashery

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

在 Visual C++ 中,如何获取当前用户的“我的文档”文件夹的路径?

Edit:

编辑:

I have this:

我有这个:

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

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

但是,result返回值为E_INVALIDARG。关于为什么会这样的任何想法?

回答by Jerry Coffin

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.

这取决于您需要兼容的系统的年龄。对于旧系统,有SHGetSpecialFolderPath。对于较新的系统,有SHGetFolderPath。从 Vista 开始,有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;
}

回答by James McNellis

Use the SHGetFolderPathWindows API functionand request CSIDL_MYDOCUMENTS.

使用SHGetFolderPathWindows API函数和要求CSIDL_MYDOCUMENTS

回答by chrisd

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 SHLoadLibraryFromKnownFolderto obtain the IShellLibraryfor the Documents library, use IShellLibrary::GetDefaultSaveFolderto get the IShellItemfor the library's default save folder, and finally use IShellItem::GetDisplayNameto get the folder name.

请注意,如果用户更改了 Win7 文档库中的默认保存文件夹,CSIDL_PERSONAL 将不会返回所需的文件夹。要获取正确的文件夹,您需要使用SHLoadLibraryFromKnownFolder获取IShellLibrary文档库的 ,使用IShellLibrary::GetDefaultSaveFolder获取IShellItem库的默认保存文件夹 ,最后使用IShellItem::GetDisplayName获取文件夹名称。

回答by maxpayne

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.

这个解决方案怎么样?它对我来说工作正常。

回答by Richard Chambers

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

在 Windows 10 下使用带有 MFC 应用程序的 Visual Studio 2017 我使用以下代码片段和SHGetKnownFolderPath 函数来获取当前用户的 Documents 文件夹:

#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\".

ppszPath [出]

类型:PWSTR*

当此方法返回时,包含指向指定已知文件夹路径的以空字符结尾的 Unicode 字符串的指针的地址。一旦不再需要该资源,调用进程负责通过调用释放该资源CoTaskMemFree。返回的路径不包含尾部反斜杠。例如,返回“C:\Users”而不是“C:\Users\”。

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

有关FOLDERID_可能的参数列表,请参阅 MSDN 文章KNOWN_FOLDER_FLAG 枚举