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

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

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.html
匿名

发表评论

匿名网友

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

确定