You know, every PC can have a different location where the user likes to store temporary files. But Windows has an API function to retrieve this directory name… and you can let Windows create a new temp-Filename that you can use.
(thanks to Rick Rogers (TeamB) for posting this hint)
Use the GetTempPath and GetTempFileName Windows API routines, like this (assuming 32-bit Delphi long strings):
[code lang=”delphi”]procedure TForm1.Button1Click(Sender: TObject);
var TempPath: string;
TempFileName: string;
Return : Integer;
begin
SetLength(TempPath, MAX_PATH);
if GetTempPath(MAX_PATH, PChar(TempPath)) = 0 then
raise Exception.Create(‘error getting temp path’);
SetLength(TempFileName, MAX_PATH);
if GetTempFileName(PChar(TempPath), ‘pre’, 0, PChar(TempFileName)) = 0 then
raise Exception.Create(‘error getting temp filename’);
TempFileName := Trim(TempFileName);
ShowMessage(TempFileName);
end;[/code]
[tags]Delphi, System, Files[/tags]
0 Kommentare zu “Getting the Temp Path of the System”