[英]How to create a path to a temporary file on Windows XP/Vista
這樣做的最佳方式是什么? tmpnam()返回驅動器根目錄中文件的路徑,這需要Windows Vista的管理員權限,因此這不是一個選項。
GetTempPath和GetTempFileName應該可以工作。
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 \\ Temp \\中的路徑,因此您不需要管理員權限。
如果您關心互操作性,tmpnam的手冊頁建議:
BUGS
Never use this function. Use mkstemp(3) instead.
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 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.