Is it possible to make an application in C# that will be able to delete itself in some condition.
I need to write an updater for my application but I don't want the executable to be left after the update process.
There is an official .Net OneClick but due to some incompatibilities with my HTTP server and some problems of OneClick itself I'm forced to make one myself.
George.
[EDIT] In more details:
I have: Application Executable which downloads the updater ("patch", but not exactly) this "patch" updates the application executable itself.
Application executes as folowed:
Application: Start -> Check Version -> Download new Updater -> Start Updater -> exit;
Updater: Start -> do it's work -> start Application Executable -> self delete (this is where I get stuck);
Mhh so let me get this straight. You got some application.exe and your updater application updater.exe?
So when you start your application.exe it checks some webserver for a newer version and then starts updater.exe. And you want updater.exe to delete itself after it has finished updating? Or do you want to delete the downloaded patch (or similar)? Please be a bit more precise.
Consider that when you are deleting updater.exe you must recreate it for the next update process.
your second line can be
If you use Process.Start you can pass in the
Del
parameter and the path to the application you wish to delete.Code snippet taken from this article
Couldn't you simply delete the updater from within the application? i.e.:
I suggest you use a batch file as a bootstrap and have it delete itself and the exe afterwards
It's tricky without introducing yet another process (that you'd then want to delete as well, no doubt). In your case, you already have 2 processes - updater.exe and application.exe. I'd probably just have the Application delete updater.exe when it's spawned from there - you could use a simple command line arg, or an IPC call from updater.exe to application.exe to trigger it. That's not exactly a self deleting EXE, but fulfills the requirements I think.
For the full treatment, and other options you should read the definitive treatment of self deleting EXEs. Code samples are in C (or ASM), but should be p/invokable.
I'd probably try CreateFile with
FILE_FLAG_DELETE_ON_CLOSE
for updater.exe with something like (psuedo code):Once application.exe exits, updater.exe has a file handle of 1. When updater.exe exits, it drops to 0 and should be deleted.