Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Self deletable application in C# in one executable

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); 
like image 557
George Avatar asked Aug 20 '09 11:08

George


2 Answers

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

like image 183
James Avatar answered Sep 18 '22 06:09

James


I suggest you use a batch file as a bootstrap and have it delete itself and the exe afterwards

public static class Updater {     public static void Main()      {            string path = @"updater.bat";          if (!File.Exists(path))          {             // Create a file to write to.             using (StreamWriter sw = File.CreateText(path))              {                 sw.WriteLine("updater.exe");                 sw.WriteLine("delete updater.exe /y");                 sw.WriteLine("delete updater.bat /y");             }               System.Process.Start(path);            }         else         {             RunUpdateProcess();         }     }      private void RunUpdateProcess()     {         .....     } } 
like image 37
3 revs, 2 users 93% Avatar answered Sep 19 '22 06:09

3 revs, 2 users 93%