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
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.
process.start("cmd /c ping localhost -n 3 > nul & del filepath") exit
replace process.start with whatever command your programming language uses to start programs with arguments
replace filepath with the path to your exe
replace exit with the command for terminating your program
It's possible to do this on Linux. You'll find that it is generally not possible to delete a running executable on Windows. However, you can have Windows delete the EXE for you on the next reboot: http://www.howtodothings.com/computers/a1402-delete-a-running-exe.html
If you want the file deleted after it's been run, you could simply ask the user to delete it. If the reason you want this is as a security measure, then what you're doing is misguided. The user could circumvent this by simply making a copy of the file first.
You can use windows scheduler to schedule a task to delete your program after X seconds.
You can run another application, which would wait for parent process to terminate, and then delete its executable.
Until that exe is in memory, it will not be able to delete itself. However, it can register with the system a task for deleting itself after a set time period of gap when it would be expected to be completing its execution.
I solved this problem (using Visual Basic) by creating a batchfile that is executed while the process is still running, waits 1sec so the program can close itself and than deletes the program.
You might need to modify it for this will delete every thing in the same folder. After your task just call del() and it should work.
Sub del() Dim file As System.IO.StreamWriter file = My.Computer.FileSystem.OpenTextFileWriter("del.bat", True) file.WriteLine("") file.WriteLine("timeout 1") file.WriteLine("echo Y | del *.*") file.Close() Process.Start("del.bat") Me.Close() End Sub
While it's not possible to self delete a file when it's running it is possible to launch a detached cmd command from the file, then end the file operation before the command executes. So, if you want to delete a bat file you can just add at the end of the file the line: start cmd /c del %0 and the file would self destruct.
The start cmd will start a new cmd window (detached from your main process). The /c tells the windows to execute whatever comes after the /c in the line. Then the del will delete the file at the path it is given. The parameter $0 refers to the first command line argument which is usually the name and path to the file that was executed, which is what we want. (the $0 parameter is the path to the file, you want to pass that to the del command).