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


Comments

Popular posts from this blog

c# console applicaton progress bar with spinner

Paging, sorting and searching in mvc + razor