gpt4 book ai didi

delphi - TDirectory.Delete 似乎是异步的

转载 作者:行者123 更新时间:2023-12-03 15:51:34 25 4
gpt4 key购买 nike

我观察到,如果要删除的文件夹中文件很少且该文件夹已打开(在 Total Commander 中),则在调用 TDirectory.Delete(x) 后立即调用 DirectoryExists(x) 返回 true。

换句话说:

begin 
TDirectory.Delete('x', true); <-- 'Delete' exited but the folder is still not fully deleted
if SysUtils.DirectoryExists('x')... <-- This returns true
end;

这是正常行为吗?

“解决方案”是这样的:

begin 
TDirectory.Delete('x', true);
Sleep(1000); //wait for Delete to finish
if SysUtils.DirectoryExists('x')...
end;

问题:我如何知道删除何时准备就绪(如何消除 sleep )?

注意:Total Commander 不会阻止删除该文件夹(我猜),因为该文件夹无论如何都会被删除(一段时间后)。

最佳答案

看看关于RemoveDirectory的msdn页面:https://msdn.microsoft.com/en-us/library/windows/desktop/aa365488(v=vs.85).aspx

备注部分说:

The RemoveDirectory function marks a directory for deletion on close. Therefore, the directory is not removed until the last handle to the directory is closed.

所以可能另一个进程也有该目录的句柄(病毒扫描程序?)。

如果您需要清空该目录,请将其清空,而不是删除它并在之后重新创建它。最终你总是要为肮脏的黑客行为付出代价;)

关于delphi - TDirectory.Delete 似乎是异步的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42809389/

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