C# Self Destruct Windows App

Here is an example of a SelfDestruct method that removes the application’s executable file after it is executed. This code is meant for applications with an output type of Windows Application. For console apps, view this post.

I usually do quick small applications to test things things that are new to me. I recently posted some Self Destruct code that I thought would work anywhere. Well, I wrote that code on a quick Console App and it did not work on a Windows App.

For it to work on a windows app, I had to allocate a console for cmd.exe to use. Sadly, I could not find a way to do this using managed code. But the managed/unmanaged code combo below works.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace SelfDestroy
{
    class Program
    {
        static void Main(string[] args)
        {
            // The program does it's thing here
            Application.Run(new Form1());

            SelfDestruct();
        }

        /// <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 SelfDestruct()
        {
            // This is only needed when the Output Type is "Windows Application"
            // because cmd.exe needs a console
            AllocConsole();

            var startInfo = new ProcessStartInfo();
            startInfo.FileName = "cmd.exe";
            startInfo.RedirectStandardInput = true;
            startInfo.UseShellExecute = false;
            startInfo.CreateNoWindow = true;

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

            // This is only needed when the Output Type is "Windows Application"
            FreeConsole();
        }
        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool AllocConsole();

        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool FreeConsole();
    }
}
Advertisements

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