1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| TWinTempFileStream=class(TFileStream)
private
FFilename :string;
function NewFilename(const ADir:string):string;
public
constructor Create(const ADir:string='');
property Filename:string read FFilename;
end;
implementation
constructor TWinTempFileStream.Create(const ADir:string);
var
H:Cardinal;
begin
FFilename:=NewFilename(ADir);
H := CreateFile(PChar(FFilename),GENERIC_READ or GENERIC_WRITE,
0, nil, CREATE_ALWAYS,
FILE_FLAG_DELETE_ON_CLOSE, 0);
if H = INVALID_HANDLE_VALUE then
raise Exception.Create('Error')
else
inherited Create(H);
end;
function TWinTempFileStream.NewFilename(const ADir: string): string;
var
FileName :array[0..MAX_PATH-1] of Char;
begin
if DirectoryExists(aDir)
then StrPCopy(FileName, aDir)
else GetTempPath(MAX_PATH, FileName);
GetTempFilename(FileName, '~__', 0, FileName);
Result := FileName;
end; |
Partager