A while ago, Marjan Venema was in need for [Archive.is] Delphi SOAP: delete temporary file after response dispatched – Stack Overflow.
The solution there is a truly temporary file: a file stream that when the handle closes will have Windows delete the file by setting the correct flags.
The construct is functionally identical to the JclFileUtils.TJclTempFileStream [Archive.is].
It passes these [Archive.is] file attribute constant flags to the [Archive.is] CreateFileW Windows API function:
FILE_ATTRIBUTE_TEMPORARYFILE_FLAG_DELETE_ON_CLOSE
I was glad she asked, though I wanted a temporary file to last after debugging, so I wrote code like this because internally the same FileGetTempName method is used by the JCL:
var
// ...
TempPath: string;
TempStream: TFileStream;
TempStreamWriter: TStreamWriter;
begin
// ...
TempPath := FileGetTempName('Prefix');
TempStream := TFile.Open(TempPath, TFileMode.fmOpenOrCreate, TFileAccess.faReadWrite, TFileShare.fsRead);
try
TempStreamWriter := TStreamWriter.Create(TempStream);
try
TempStreamWriter.WriteLine('Debug starts:');
MyStringList.SaveToStream(TempStream);
TempStreamWriter.WriteLine();
// ...
TempStreamWriter.WriteLine('Debug finishes.');
finally
TempStreamWriter.Free();
end;
finally
TempStream.Free();
end;
–jeroen






