这样做的最佳方法是什么?tmpnam() 返回驱动器根目录中文件的路径,这需要 Windows Vista 上的管理员权限,因此这不是一个选项。

有帮助吗?

解决方案

其他提示

Windows 上的环境变量 %TEMP% 指向用户临时目录。

在托管 C++ 中,您可以调用 Path::GetTempFileName() ,它将在用户临时目录中提供一个临时文件(可以使用 Path::GetTempPath() 找到该文件)。GetTempFileName() 基本上只是使用 GUID 作为文件名为您提供 %TEMP% 路径中文件的路径。然后,您可以使用该路径创建文件并对其执行您想要的操作。您可以使用任何可以访问当前进程环境变量的语言来执行类似的逻辑。

希望有帮助,

马丁.

您是否尝试过将环境变量 TEMP 和 TMP 设置为所有人都可写的目录?要在XP(不熟悉Vista)中更改环境变量,请转到系统属性,[高级]选项卡,[环境变量]按钮。

也许您可以使用 kernel32.dll 中的 Win32 方法 GetTempPath()。它通过 System.IO.Path.GetTempFileName() 包装在 .NET 中。

在 XP 上,这将返回 C:\Documents and Settings\username\Local Settings emp\ 中的路径,因此您不需要管理员权限。

如果您关心互操作性,tmpnam 的手册页建议:

tmpnam 手册页

BUGS
       Never use this function. Use mkstemp(3) instead.

mkstemp 手册页

SYNOPSIS
       #include <stdlib.h>

       int mkstemp(char *template);

DESCRIPTION
       The mkstemp() function generates a unique temporary file name from template.  The last six characters of template must be
       XXXXXX and these are replaced with a string that makes the filename unique. The file is then created with mode read/write

但所有这些都表明您已经准备好了以 TMP 环境变量的内容为前缀的模板。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top