在Windows中获取用户临时文件夹路径

时间:2009-08-24 13:32:47

标签: c++ windows path directory

如何在C ++中获取用户的临时文件夹路径?我的程序必须在Windows Vista和XP上运行,并且它们具有不同的临时路径。如何在不失去兼容性的情况下获得它?

9 个答案:

答案 0 :(得分:21)

您是否有理由不能使用Win32 GetTempPath API?

此API从W2K开始提供,因此可用于所有列出的目标。

答案 1 :(得分:6)

GetTempPath函数检索为临时文件指定的目录的路径。此函数取代GetTempDrive函数。

DWORD GetTempPath(

DWORD nBufferLength, // size, in characters, of the buffer 
LPTSTR lpBuffer // address of buffer for temp. path 
); 

<强>参数

nBufferLength

指定lpBuffer标识的字符串缓冲区的大小(以字符为单位)。

lpBuffer

指向字符串缓冲区,该字符串缓冲区接收以空值终止的字符串,指定临时文件路径。

返回值

如果函数成功,则返回值是复制到lpBuffer的字符串的长度(以字符为单位),不包括终止空字符。如果返回值大于nBufferLength,则返回值是保存路径所需的缓冲区大小。 如果函数失败,则返回值为零。要获取扩展错误信息,请调用GetLastError。

<强>说明

GetTempPath函数获取临时文件路径,如下所示:

  1. TMP环境变量指定的路径。
  2. 如果未定义TMP,则由TEMP环境变量指定的路径。
  3. 当前目录,如果未定义TMP和TEMP。

答案 2 :(得分:4)

从C ++ 17开始,您可以使用跨平台功能: std::filesystem::temp_directory_path()

https://en.cppreference.com/w/cpp/filesystem/temp_directory_path

答案 3 :(得分:3)

#include <iostream>
#include <string>

int main(int argc, char* argv[]){

    std::cout << getenv("TEMP") << std::endl;

    return 0;
}

答案 4 :(得分:2)

使用GetTempPath()检索为临时文件指定的目录的路径。

wstring TempPath;
wchar_t wcharPath[MAX_PATH];
if (GetTempPathW(MAX_PATH, wcharPath))
   TempPath = wcharPath;

答案 5 :(得分:1)

除非用户具有管理访问权限,否则GetTempPath不会在Vista上运行。我现在正在使用我的一个应用程序遇到这个问题。

答案 6 :(得分:0)

Function GetTempPath will return a path with a short name,eg: C:\Users\WDKREM~1\AppData\Local\Temp\.

To get a full temp path name,use GetLongPathName subsequently.

答案 7 :(得分:0)

正如VictorV所指出的,GetTempPath返回了一条折叠路径。您需要同时使用GetTempPathGetLongPathName宏来获取完全展开的路径。

std::vector<TCHAR> collapsed_path;
TCHAR copied = MAX_PATH;
while ( true )
{
    collapsed_path.resize( copied );
    copied = GetTempPath( collapsed_path.size( ), collapsed_path.data( ) );
    if ( copied == 0 ) 
        throw std::exception( "An error occurred while creating temporary path" );
    else if ( copied < collapsed_path.size( ) ) break;
}

std::vector<TCHAR> full_path;
copied = MAX_PATH;
while ( true )
{
    full_path.resize( copied );
    copied = GetLongPathName( collapsed_path.data( ), full_path.data( ), full_path.size( ) );
    if ( copied == 0 ) 
        throw std::exception( "An error occurred while creating temporary path" );
    else if ( copied < full_path.size( ) ) break;
}
std::string path( std::begin( full_path ), std::end( full_path ) );

答案 8 :(得分:0)

在Windows 10中,这可能很棘手,因为“临时路径”的值不仅取决于其默认设置,还取决于您使用的是哪种类型的应用程序。因此,这取决于您的具体需求。

用户的本地应用程序数据中的

[公共区域] TEMP

#include <Windows.h>
#include <Shlobj.h>
#include <Shlobj_core.h>
#include <string_view>
// ...
static void GetUserLocalTempPath(std::wstring& input_parameter) {
    static constexpr std::wstring_view temp_label = L"\\Temp\\";
    HWND folder_handle = { 0 };
    WCHAR temp_path[MAX_PATH];
    auto get_folder = SHGetFolderPath( 
        folder_handle, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_DEFAULT, temp_path
    );
    if (get_folder == S_OK) {
        input_parameter = static_cast<const wchar_t*>(temp_path);
        input_parameter.append(temp_label);
        CloseHandle(folder_handle);
    }
}

GetUserLocalTempPath可能会返回全名而不是简称。
另外,如果正在运行的任何东西都以SYSTEM身份而不是登录用户的身份运行,而不是返回%USERPROFILE%\AppData\Local\Temp,它将返回类似C:\Windows\System32\config\systemprofile\AppData\Local\Temp

的信息。

对于TEMP环境变量来说都是温度

#include <Windows.h>
// ...
static void GetEnvTempPath(std::wstring& input_parameter) {
    wchar_t * env_var_buffer = nullptr;
    std::size_t size = 0;
    if ( _wdupenv_s(&env_var_buffer, &size, L"TEMP") == 0 &&
         env_var_buffer != nullptr) {
        input_parameter = static_cast<const wchar_t*>(env_var_buffer);
    }
}

[Robust] Temp(无论您的应用可以访问什么(C ++ 17)

#include <filesystem>
// ...
auto temp_path = std::filesystem::temporary_directory_path().wstring();

temporary_directory_path可能会返回短名而不是全名。


根据您的需要,您可能会充分利用前几个功能和最后一个功能。如果要处理AppContainer应用程序,请使用<filesystem>提供的最后一个。它应该返回类似

的内容

C:\Users\user name\AppData\Local\Packages\{APP's GUID}\AC\Temp