TCHAR szPath[MAX_PATH] = {0};
SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, szPath);
CString strPath = szPath;
strPath += _T("\\temp"); // 在「我的文檔」下創建 temp
URLDownloadToFile(NULL, REGISTER_URL, strPath, 0, NULL);
嘗試改為 strPath += _T("e:\\temp") 期望在 E 盤創建,結果兩邊都不創建。
SHGetFolderPath 將「我的文檔」路徑寫入 szPath,賦值給 strPath 後,+= 是在原路徑後拼接。直接 += _T("e:\\temp") 會變成 C:\Users\...\Documents\e:\temp,路徑非法。
// 直接覆蓋,不再使用 SHGetFolderPath 返回值
strPath = _T("e:\\temp");
// 或者保留獲取邏輯,但根據條件決定:
// if (useCustom) strPath = _T("e:\\temp"); else strPath += _T("\\temp");
若需確保目錄存在,下載前調用 SHCreateDirectoryEx(NULL, strPath, NULL) 或 CreateDirectory。