Search code examples
c++winapiwixwindows-installerdelete-file

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


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

Solution

  • 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: How can I force the deletion of locked files in C/C++?