unique temp file name/de
Zurück zur Seite Code Beispiele.
Dieser Artikel beschäftigt sich mit der Windowsprogrammierung.
Die Funktion generiert für temporäre Dateien einen eindeutigen Dateinamen.
uses
SysUtils, ...;
...
function TempFileName(strPrefix: String): string;
var
pchBuffer: PChar;
pchTempDir: PChar;
pchPrefix: PChar;
const
wrdTempDirSize = 255;
wrdBufferSize = 255;
wrdPrefixSize = 3;
begin
// Speicher anfordern
GetMem(pchBuffer, wrdBufferSize);
GetMem(pchPrefix,wrdPrefixSize);
GetMem(pchTempDir,wrdTempDirSize);
// kürzt das Prefix auf die zulässigen 3 Stellen
StrPCopy(pchPrefix,Copy(strPrefix,0,3));
// kopiert den Inhalt der Umgebungsvariable in ein PChar
StrPCopy(pchTempDir,GetEnvironmentVariable('Temp'));
if GetTempFileName(pchTempDir, pchPrefix, 0, pchBuffer) > 0 then
Result := StrPas(pchBuffer)
else
Result := '';
// Speicher freigeben
FreeMem(pchBuffer, wrdBufferSize);
FreeMem(pchPrefix, wrdPrefixSize);
FreeMem(pchTempDir, wrdTempDirSize);
end;
...oder einfacher:
uses
...Sysutils...
function TempFileName(strPrefix: String): string;
begin
result:=SysUtils.GetTempFileName('',strPrefix);
end;From the Free Pascal wiki, page "unique temp file name/de", as it stood on 2020-03-02 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.
Edited on wiki.freepascal.org by 5 people, 2013–2020
From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.
| When | Who | What changed |
|---|---|---|
| Olaf | Created page | |
| BigChimp | Perhaps it pays to investigate the RTL a bit more to see if functions are already present. | |
| BigChimp | ||
| Olaf | (minor) | |
| Billyraybones | (minor) | |
| Olaf | (minor) | |
| FTurtle | ||
| Trev | Fixed syntax highlighting (minor) | |
| Trev | Deleted category included in page template (minor) |
Talk about this page
Nobody has opened a bench for this page yet. Sign in to open one.
