简体   繁体   English

GetTempFileName 创建一个空文件

[英]GetTempFileName creates an empty file

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...我不确定我是否是第一个注意到这一点的人,但是(大声笑,这有点有趣......)当我打电话时(我调试了应用程序以验证这一点)GetTempFileName(来自 Windows.pas 单元),它会在我的 D:\\(不知道为什么在那里)驱动器中创建一个空(0 字节)文件...

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 ('..').我有 2 个逻辑驱动器... C:\\ (Primary) 和 D:\\ (Logical),现在只有当我调用它时第一个参数 (arg0) 只是一个点 ('.') 时才会发生这种情况,这很奇怪对我来说,它在 D:\\ 中创建它,因为父目录是 2 个点('..')。

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 ? Edit1:我的问题是,为什么会这样? 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;

Why does GetTempFileName function create new empty file ?为什么 GetTempFileName 函数会创建新的空文件?

It's in the GetTempFileName function description (emphasized by me):它在GetTempFileName函数描述中(我强调):

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 ?使用 GetTempFileName 函数时如何避免创建新文件?

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.要么不能让函数生成唯一的文件名(这会创建一个空文件),要么在GetTempFileName函数返回后删除该创建的空文件。 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 .要删除这些文件,请调用 DeleteFile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM