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);
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
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() { ..... } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With