C++:不带波浪号的 GetTempPath() (~)

C++: GetTempPath() without tilde (~)

我需要获取临时路径。所以我尝试了 GetTempPath() 和 std::getenv("TEMP")。问题是结果中包含~符号,例如C:\Users\OLEKSI~1.ALE\AppData\Local\Temp。 是否有可能获得完整的临时路径,如 C:\Users\Oleksii\AppData\Local\Temp?

根据the Microsoft documentation

The GetTempPath function checks for the existence of environment variables in the following order and uses the first path found:

  • The path specified by the TMP environment variable.
  • The path specified by the TEMP environment variable.
  • The path specified by the USERPROFILE environment variable.
  • The Windows directory.

您检查过这些设置的内容了吗?在Windows10中可以按Windows+Break打开系统设置,然后点击右侧的高级系统设置,再点击环境变量。如果 TMP、TEMP 或 USERPROFILE 在那里,请检查它们是否设置正确并且其中没有不需要的 ~。如果不是,请尝试添加它们。

GetLongPathName 函数:

Converts the specified path to its long form.

DWORD GetLongPathNameW(
  [in]  LPCWSTR lpszShortPath,
  [out] LPWSTR  lpszLongPath,
  [in]  DWORD   cchBuffer
);

DWORD GetLongPathNameA(
  [in]  LPCSTR lpszShortPath,
  [out] LPSTR  lpszLongPath,
  [in]  DWORD  cchBuffer
);

设置 lpszShortPath = 带有 ~ 符号的短路径。从 lpszLongPath.

读取结果

GetLongPathName