我的代码是:

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 [输入]
  
  类型:int
  
  CSIDL值,用于标识要检索其路径的文件夹。仅真实文件夹有效。如果指定了虚拟文件夹,此功能将失败。您可以通过组合文件夹的CSIDL和CSIDL_FLAG_CREATE来强制创建文件夹。


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

最佳答案

正如SHGetFolderPath文档明确指出的那样,nFolder参数是整数,而不是字符串。 CSIDL值是数字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);
    }

10-07 20:37