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

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

Visual Studio 2008 Freezes in ASPX Source View

I recently installed Office 2010 on my machine and VS 2008 began freezing up when displaying the source code of aspx pages.

VS would lock down when you clicked or when you moved the cursor using the arrow keys on the Source code view of an aspx page. Once it was locked, all mouse clicks would be followed by the Windows beeping sound that you hear when you try to click outside a dialog window.

One other detail that identifies this specific problem is that when Visual Studio hangs it happens to spawn a Setup process that never exits.

This setup.exe is located in the following directory:

C:\Program Files (x86)\Common Files\microsoft shared\OFFICE12\Office Setup Controller\Setup.exe

My problem is extremely similar to the problem this guy was having when he installed Office 2007. He was able to fix his by reinstalling Office 2007. I noticed the setup.exe thing because of his post.

I tried repairing Office 2010 and VS 2008 with no success. So I ended up downgrading to Office 2007. This is the order and weird things I did that worked for me.

1. Uninstall VS 2008

2. Uninstall Office 2010

3. Deleted the following folders:

  • C:\Program Files (x86)\Common Files\microsoft shared\OFFICE11
  • C:\Program Files (x86)\Common Files\microsoft shared\OFFICE12
  • C:\Program Files (x86)\Common Files\microsoft shared\OFFICE14

4. Installed Office 2007

5. Installed VS 2008

6. Ran the windows updates for Office 2007 and VS 2008.