IceCold
IceCold

Reputation: 21194

How to delete the folder in which the application (self) is running?

From an application I want to delete a folder. The problem is that the folder also contains that application. So, as long as the application is running the folder is locked. How can I delete that folder?

The first thought was to make a copy of that application in 'Temp' and run it from there. This way it it will release the folder. Not a very elegant solution though....

The OS is Win7 but it should work on all from Win2K up to Win 8. The goal is to let the application to self-uninstall (clean up its folder, then self delete).


Somebody suggested that if I do this in my app it will work. It doesn't.

SetCurrentDir(SomeOtherFolder);

Upvotes: 1

Views: 235

Answers (1)

David Heffernan
David Heffernan

Reputation: 613282

On Windows, the executable file is locked while the process executes in such a way that the file cannot be deleted. So you will need to wait until the process has stopped executing before you can delete the executable file.

There are two options that appear to me to be viable:

  1. Use another process to perform the deletion. That process can wait until the target process has stopped and then perform deletion.
  2. Use MoveFileEx passing the MOVEFILE_DELAY_UNTIL_REBOOT flag and nil for the destination.

Upvotes: 2

Related Questions