如何在cpp win32中删除当前正在被另一个进程使用的文件?

huangapple go评论105阅读模式
英文:

How can I delete a file that is currently in use by another process in cpp win32?

问题

在卸载我的msi应用程序时,调用DeleteFile()函数后,Program Data文件夹中的日志文件(.txt)未被删除。通过使用GetErrorCode(),我可以检查它返回错误代码32,这意味着文件正在使用中。有没有办法在卸载时删除这些日志文件?理论或源代码 - 任何形式的帮助将不胜感激。

我尝试使用下面的代码,但没有成功。甚至fclose()操作也没有产生任何积极的结果。

SHFILEOPSTRUCT file_op = {
        NULL,
        FO_DELETE,
        dir,
        "",
        FOF_NOCONFIRMATION |
        FOF_NOERRORUI |
        FOF_SILENT,
        false,
        0,
        "" };
    SHFileOperation(&file_op);
英文:

While uninstalling my msi application , the log files(.txt) in Program Data folder are not getting deleted after calling DeleteFile() function. By using GetErrorCode() I could check its returning error code 32 which implies the file is in use.Is there a way I can delete these log files while uninstallation? Theory or source -Any kind of help would be appreciated.

I tried using the below code but it didn't work. Even fclose() operation didn't yield any positive results.

SHFILEOPSTRUCT file_op = {
        NULL,
        FO_DELETE,
        dir,
        "",
        FOF_NOCONFIRMATION |
        FOF_NOERRORUI |
        FOF_SILENT,
        false,
        0,
        "" };
    SHFileOperation(&file_op);

答案1

得分: 0

你无法删除正在被另一个进程使用的文件。您可以尝试使用MoveFileEx并带有MOVEFILE_DELAY_UNTIL_REBOOT标志。

有关更多详细信息,建议您参考以下线程:
https://stackoverflow.com/questions/215461/how-can-i-force-the-deletion-of-locked-files-in-c-c#215466

英文:

You couldn't delete a file that is currently in use by another process. You could try to use Use MoveFileEx with the MOVEFILE_DELAY_UNTIL_REBOOT flag.

For more details I suggest you could refer to the threads:
https://stackoverflow.com/questions/215461/how-can-i-force-the-deletion-of-locked-files-in-c-c#215466

huangapple
  • 本文由 发表于 2023年8月9日 14:05:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864977-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定