GetTempFileName creates an empty file
delphi, winapi
Solution
Why does GetTempFileName function create new empty file ?
It's in the `GetTempFileName` function description (emphasized by me):
Creates a name for a temporary file. If a unique file name is generated, an empty file is created and the handle to it is released; otherwise, only a file name is generated.
How to avoid creating the new file when using GetTempFileName function ?
Either you can't let the function generate a unique file name (which creates an empty file) or just delete that created empty file after the `GetTempFileName` function returns. From the reference (emphasized by me):
Temporary files whose names have been created by this function are not automatically deleted. To delete these files call DeleteFile.
Problem
i'm not sure if i'm the first one to notice this, but (lol, it's kinda funny...) when ever i make a call (i debugged the application to verify this) GetTempFileName (from the Windows.pas unit), it creates an empty (0 byte) file in my D:\ (no idea why there) drive... i have 2 logical drives... C:\ (Primary) and D:\ (Logical), now this ONLY happens if i call it when the first argument (arg0) is just a dot ('.'), which is weird to me that it creates it in D:\ since parent directory is 2 dots ('..'). anyways, a dummy bypass for this is to just delete the newely created file... -_-, all i need is the file NAME, not the file system itself... Edit1: My question is, why is this happening ? and is there a way to avoid the call from creating a new empty file ? ``` // Creates a name for a temporary file. Function GetTempFileName(const ext: string): string; var // This buffer should be MAX_PATH characters to accommodate the path plus the terminating null character. lpTempFileName: Array[0..MAX_PATH] of char; begin // If uUnique is zero, GetTempFileName creates an empty file and closes it. // If uUnique is not zero, you must create the file yourself. Only a file name is created Windows.GetTempFileName('.', nil, 0, lpTempFileName); DeleteFile(lpTempFileName); // delete created file Result := ChangeFileExt(lpTempFileName, ext); Delete(Result, 1, 2); // ".\" end; ```