gpt4 book ai didi

delphi - GetTempFileName 创建一个空文件

转载 作者:行者123 更新时间:2023-12-03 18:05:49 25 4
gpt4 key购买 nike

我不确定我是否是第一个注意到这一点的人,但是(哈哈,这有点有趣......)每当我打电话时(我调试应用程序来验证这一点)GetTempFileName(来自 Windows .pas 单元),它在我的 D:\(不知道为什么)驱动器中创建一个空(0 字节)文件...

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

无论如何,一个虚拟的绕过就是删除新创建的文件...-_-,我只需要文件名,而不是文件系统本身...

Edit1:我的问题是,为什么会这样?有没有办法避免调用创建新的空文件?

// 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;

最佳答案

为什么 GetTempFileName 函数会创建新的空文件?

它在 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.


如何避免在使用 GetTempFileName 函数时创建新文件?

要么您不能让该函数生成一个唯一的文件名(这会创建一个空文件),要么只是删除在 GetTempFileName 之后创建的空文件函数返回。来自引用文献(我强调):

Temporary files whose names have been created by this function are not automatically deleted. To delete these files call DeleteFile.

关于delphi - GetTempFileName 创建一个空文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15312704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com