unique temp file name/de: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: unique temp file name/de
| Old | New | Line | |
|---|---|---|---|
| 1 | + | Zurück zur Seite [[code examples/de|Code Beispiele]]. | |
| 2 | + | ||
| 3 | + | Dieser Artikel beschäftigt sich mit der Windowsprogrammierung. | |
| 4 | + | ||
| 5 | + | Die Funktion generiert für temporäre Dateien einen eindeutigen Dateinamen. | |
| 6 | + | ||
| 7 | + | ``` pascal | |
| 8 | + | uses | |
| 9 | + | SysUtils, ...; | |
| 10 | + | ||
| 11 | + | ... | |
| 12 | + | ||
| 13 | + | function TempFileName(strPrefix: String): string; | |
| 14 | + | var | |
| 15 | + | pchBuffer: PChar; | |
| 16 | + | pchTempDir: PChar; | |
| 17 | + | pchPrefix: PChar; | |
| 18 | + | ||
| 19 | + | const | |
| 20 | + | wrdTempDirSize = 255; | |
| 21 | + | wrdBufferSize = 255; | |
| 22 | + | wrdPrefixSize = 3; | |
| 23 | + | ||
| 24 | + | begin | |
| 25 | + | // Speicher anfordern | |
| 26 | + | GetMem(pchBuffer, wrdBufferSize); | |
| 27 | + | GetMem(pchPrefix,wrdPrefixSize); | |
| 28 | + | GetMem(pchTempDir,wrdTempDirSize); | |
| 29 | + | ||
| 30 | + | // kürzt das Prefix auf die zulässigen 3 Stellen | |
| 31 | + | StrPCopy(pchPrefix,Copy(strPrefix,0,3)); | |
| 32 | + | // kopiert den Inhalt der Umgebungsvariable in ein PChar | |
| 33 | + | StrPCopy(pchTempDir,GetEnvironmentVariable('Temp')); | |
| 34 | + | ||
| 35 | + | if GetTempFileName(pchTempDir, pchPrefix, 0, pchBuffer) > 0 then | |
| 36 | + | Result := StrPas(pchBuffer) | |
| 37 | + | else | |
| 38 | + | Result := ''; | |
| 39 | + | ||
| 40 | + | // Speicher freigeben | |
| 41 | + | FreeMem(pchBuffer, wrdBufferSize); | |
| 42 | + | FreeMem(pchPrefix, wrdPrefixSize); | |
| 43 | + | FreeMem(pchTempDir, wrdTempDirSize); | |
| 44 | + | end; | |
| 45 | + | ||
| 46 | + | ... | |
| 47 | + | ||
| 48 | + | ``` | |
| 49 | + | ||
| 50 | + | oder einfacher: | |
| 51 | + | ||
| 52 | + | ``` pascal | |
| 53 | + | uses | |
| 54 | + | ...Sysutils... | |
| 55 | + | function TempFileName(strPrefix: String): string; | |
| 56 | + | begin | |
| 57 | + | result:=SysUtils.GetTempFileName('',strPrefix); | |
| 58 | + | end; | |
| 59 | + | ``` | |
| 60 | + | ||
| 61 | + | --- | |
| 62 | + | *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](/wiki-contact).* |
