2

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);
  • 2
    Windows doesn't allow you to remove a file which is opened by any process, even your own. To be able to remove a file, all references to the file must be closed and removed first. – Some programmer dude Aug 09 '23 at 06:10
  • 3
    You are probably looking for this: https://stackoverflow.com/a/5490755/898348 – Jabberwocky Aug 09 '23 at 06:20
  • Does this answer your question? [How do I copy a file or folder that is locked under windows programmatically?](https://stackoverflow.com/questions/259253/how-do-i-copy-a-file-or-folder-that-is-locked-under-windows-programmatically) – Simon Mourier Aug 09 '23 at 06:28
  • Thanks for the help but I need to know which process is holding my file so that I can close the process handle.Any way to do that? – Abhiroop Saha Aug 10 '23 at 06:12
  • This is another question https://stackoverflow.com/questions/18450381/windows-event-viewer-holds-a-lock-on-my-exe-file/18452006#18452006 – Simon Mourier Aug 10 '23 at 07:27

1 Answers1

0

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++?

Jeaninez - MSFT
  • 3,210
  • 1
  • 5
  • 20