Title: Temporary files in Windows
Question: Temporary files in Windows
Answer:
Windows provides us a series of tools to work with temporary files in our application.
The first thing that we need to know it is the path of the temporal directory of the system, that which we can obtain by means of the function of the API: GetTempPath
The second thing that we need it is to obtain an aleatory name for our temporal file.
In multithread applications that need several temporal files simultaneously, it is good idea to let that Windows gives us the names of the temporal files to create.
Here you have a function that every time that you execute it, will return you a file name pointing toward the temporary directory of Windows:
-Put a TMemo (Memo1) and a TButton (Button1) in your form
-Put this code un the OnCLick of Button1:
procedure TForm1.Button1Click(Sender: TObject);
function CreateTmpFileName(Prefijo: String): String;
var
Path : array[0..MAX_PATH] of Char;
Fichero : array[0..MAX_PATH] of Char;
begin
FillChar(Path,SizeOf(Path),#0);
FillChar(Fichero,SizeOf(Fichero),#0);
GetTempPath(MAX_PATH,
Path);
GetTempFilename(Path,
PChar(Prefijo),
0,
Fichero);
Result := Fichero;
end;
begin
Memo1.Lines.Append( CreateTmpFileName('TmpF') );
end;