简体   繁体   English

如何在Windows XP / Vista上创建临时文件的路径

[英]How to create a path to a temporary file on Windows XP/Vista

What is the best way of doing this? 这样做的最佳方式是什么? tmpnam() returns a path to a file in the root of the drive, which requires administrator privileges on Windows Vista, so this is not an option. tmpnam()返回驱动器根目录中文件的路径,这需要Windows Vista的管理员权限,因此这不是一个选项。

GetTempPathGetTempFileName应该可以工作。

The environment variable %TEMP% on Windows points to the users temp directory. Windows上的环境变量%TEMP%指向用户临时目录。

In managed C++ you can call Path::GetTempFileName() which will give you a temporary file in the users temp directory (which can be found using Path::GetTempPath() ). 在托管C ++中,您可以调用Path :: GetTempFileName(),它将在用户临时目录(可以使用Path :: GetTempPath()找到)中为您提供临时文件。 GetTempFileName() basically just gives you a path to a file in the %TEMP% path using a GUID as the file name. GetTempFileName()基本上只使用GUID作为文件名为您提供%TEMP%路径中文件的路径。 You then use that path to create the file and do what you want with it. 然后,您可以使用该路径创建文件并使用它执行所需的操作。 You could do similar logic in any language that has access to the current processes environment variables. 您可以在任何可以访问当前进程环境变量的语言中执行类似的逻辑。

Hope that helps, 希望有所帮助,

Martin. 马丁。

Have you tried with the environment variables TEMP and TMP set to a directory writable by all? 您是否尝试过将环境变量TEMP和TMP设置为所有人都可写的目录? To change environment variables in XP (not familiar with Vista), you go to System Properties, [Advanced] tab, [Environment Variables] button. 要更改XP中的环境变量(不熟悉Vista),请转到“系统属性”,“[高级]”选项卡,“环境变量”按钮。

Perhaps you could use the Win32 method GetTempPath() in kernel32.dll. 也许你可以在kernel32.dll中使用Win32方法GetTempPath()。 This is wrapped in .NET by System.IO.Path.GetTempFileName(). 它由System.IO.Path.GetTempFileName()包装在.NET中。

On XP this returns a path in C:\\Documents and Settings\\username\\Local Settings\\Temp\\, so you should not require admin privileges. 在XP上,它返回C:\\ Documents and Settings \\ username \\ Local Settings \\ Temp \\中的路径,因此您不需要管理员权限。

If you care about interoperability, the man page for tmpnam suggests: 如果您关心互操作性,tmpnam的手册页建议:

tmpnam man page tmpnam手册页

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

mkstemp man page 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

but all these suggest that you have prepared your template prefixed by the contents of the TMP environment variable. 但所有这些都表明您已准备好以TMP环境变量的内容为前缀的模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM