Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a program delete its own executable

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

like image 750
ata Avatar asked Oct 22 '09 09:10

ata


People also ask

Can an executable delete itself?

The Self-Deleting Batch File methodWhen the execuable has been deleted, the batch file then deletes itself. The executable needs to spawn off the batch file using CreateProcess , and then should exit immediately.

Why does my computer automatically delete EXE files?

Reasons for EXE Files Loss The most apparent reason for the issue “lost EXE file” is antivirus settings or virus or malware infection. Most of the viruses or malware are delivered through executable files, and the antivirus programs always make most of the executable files suspicious and block or even delete them.

What creates an executable file?

EXE files are a Windows-specific executable file format. When a user or other event triggers an executable file, the computer runs the code that the file contains. Executable files contain binary machine code that has been compiled from source code.

How do I delete a running EXE?

Double-click the executable file to run FileASSASIN. Click the “Browse” button to find files to delete. After selecting a file to delete, FileASSASIN will provide you with several options. Select the “Delete” option if you want to erase the file.


1 Answers

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.

like image 147
Greg Hewgill Avatar answered Oct 10 '22 18:10

Greg Hewgill