下面的函数返回一个临时文件名:
{ Create:是否创建文件,PreFix:文件名前缀,Ext:文件扩展名 }
function GetTmpFileName ( Create : Boolean = False ; Prefix : string = '~~' ; Ext : string = '.TMP' ):
string ;
var
Buffer : string ;
Len : integer ;
UID : integer ;
begin
Len := GetTempPath ( 0 , nil ) + 1 ; { Get Temp Path Length }
SetLength ( Buffer , Len ); { Get Memory for Buffer }
GetTempPath ( Len , @ Buffer [ 1 ]); { Get Temp Path }
SetLength ( Result , MAX_PATH );
Randomize ;
if Create then
UID := 0
else
UID := Random ( $FFFFFFFF ) + 1 ;
repeat
GetTempFileName ( pchar ( Buffer ), pchar ( Prefix ), UID , @ Result [ 1 ]);
Result := ChangeFileExt ( Result , Ext );
until ( not FileExists ( Result )) or Create ;
end ;