使用带有SHGetFolderPath的数组

时间:2016-06-15 08:02:17

标签: c++ windows winapi

我的代码是:

int copyme() {

    TCHAR buf[MAX_PATH];
    TCHAR szPath[MAX_PATH];
    std::array<std::string, 16> locations = { "CSIDL_APPDATA","CSIDL_CDBURN_AREA","CSIDL_COMMON_APPDATA","CSIDL_COMMON_DESKTOPDIRECTORY","CSIDL_COMMON_DOCUMENTS","CSIDL_COMMON_MUSIC","CSIDL_COMMON_PICTURES","CSIDL_COMMON_PROGRAMS","CSIDL_COMMON_STARTMENU","CSIDL_COMMON_STARTUP","CSIDL_COMMON_TEMPLATES","CSIDL_COMMON_VIDEO","CSIDL_DESKTOPDIRECTORY","CSIDL_INTERNET_CACHE","CSIDL_LOCAL_APPDATA","CSIDL_PERSONAL", };

    for (auto& location : locations) {

        (SHGetFolderPath(NULL,
            location | CSIDL_FLAG_CREATE,
            NULL,
            0,
            szPath)
    }

我正在尝试使用数组中的CSIDL调用SHGetFolderPath,但根据文档:

  

nFolder [in]

     

输入:int

     

CSIDL值,用于标识要检索其路径的文件夹。只有真正的文件夹才有效。如果指定了虚拟文件夹,则此功能将失败。您可以通过将文件夹的CSIDL与CSIDL_FLAG_CREATE组合来强制创建文件夹。

因此只能使用int。有什么想法我可以做什么?

1 个答案:

答案 0 :(得分:4)

正如samples文档明确指出的那样,nFolder参数是整数,而不是字符串。 SHGetFolderPath值是数字ID,而不是字符串(#define中的人类可读名称为shlobj.h

因此,只需将数组从std::string更改为int,然后删除CSIDL名称周围的引号字符:

int copyme()
{
    TCHAR szPath[MAX_PATH];
    std::array<int, 16> locations = {
        CSIDL_APPDATA, CSIDL_CDBURN_AREA, CSIDL_COMMON_APPDATA, CSIDL_COMMON_DESKTOPDIRECTORY,
        CSIDL_COMMON_DOCUMENTS, CSIDL_COMMON_MUSIC, CSIDL_COMMON_PICTURES, CSIDL_COMMON_PROGRAMS,
        CSIDL_COMMON_STARTMENU, CSIDL_COMMON_STARTUP, CSIDL_COMMON_TEMPLATES, CSIDL_COMMON_VIDEO,
        CSIDL_DESKTOPDIRECTORY, CSIDL_INTERNET_CACHE, CSIDL_LOCAL_APPDATA, CSIDL_PERSONAL};

    for (auto location : locations)
    {
        SHGetFolderPath(NULL,
            location | CSIDL_FLAG_CREATE,
            NULL,
            0,
            szPath);
    }