Self deletable application in C# in one executable

George picture George · Aug 20, 2009 · Viewed 37.4k times · Source

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);

Answer

James picture James · Aug 20, 2009

If you use Process.Start you can pass in the Del parameter and the path to the application you wish to delete.

ProcessStartInfo Info=new ProcessStartInfo();
Info.Arguments="/C choice /C Y /N /D Y /T 3 & Del "+
               Application.ExecutablePath;
Info.WindowStyle=ProcessWindowStyle.Hidden;
Info.CreateNoWindow=true;
Info.FileName="cmd.exe";
Process.Start(Info); 

Code snippet taken from this article