How can a program delete its own executable

file, process, windows

Solution

One way to do this is to use the MoveFileEx function with the `MOVEFILE_DELAY_UNTIL_REBOOT` flag and a `NULL` destination. According to the documentation, this:

registers the lpExistingFileName file to be deleted when the system restarts. If lpExistingFileName refers to a directory, the system removes the directory at restart only if the directory is empty.

Problem

Is there any way that a running process can delete its own executable? For example, I make a console application (single exe) and after doing some tasks it somehow deletes the exe file. I have to send a single file to someone. And I want it deleted after it does its intended task. Is there anyway to do it in Windows

Original source

Related problems