C# Self Destruct Console App

Just call this method at the very end of your C# application to delete application’s executable file.

        /// <summary>
        /// This method deletes the exe that calls it.
        /// 
        /// This method must be called right at the end of the application.
        /// </summary>
        private static void SelfDestroy()
        {
            var startInfo = new ProcessStartInfo();
            startInfo.FileName = "cmd.exe";
            startInfo.RedirectStandardInput = true;
            startInfo.UseShellExecute = false;

            var process = new Process();
            process.StartInfo = startInfo;
            process.Start();

            // The delay is just making sure the exe to delete is done
            // running.
            var delayPings = 2;
            var exeName = AppDomain.CurrentDomain.FriendlyName;
            process.StandardInput.WriteLine("(ping -n " + delayPings + " 127.0.0.1) && (del /Q " + exeName + ")");
        }
Advertisements

One thought on “C# Self Destruct Console App

  1. Pingback: C# Self Destruct Windows App « Pedro Liska's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s