C# Code Snippets

Delete Self

Max Black

Uses a batch script that it creates to delete your executable, then deletes itself.

Platform: .NET Framework 2.0

using System.Diagnostics;
using System.IO;
static private void DeleteSelf()
{
    //Use this line if your running from console
    //string pa = Process.GetCurrentProcess().MainModule.FileName;

    //Use this line if your running from Windows Form
    string pa = Application.StartupPath;
    string bf = "@echo off" + Environment.NewLine + 
                ":dele" + Environment.NewLine + 
                "del \"" + pa + "\"" + Environment.NewLine + 
                "if Exist \"" + pa + "\" GOTO dele" + Environment.NewLine + 
                "del %0";

    //string filename = RandomString(RandomNumber(5, 15)) + ".bat";
    string filename = Path.GetRandomFileName() + ".bat";

    StreamWriter file = new StreamWriter(Environment.GetEnvironmentVariable("TMP") + filename);
    file.Write(bf);
    file.Close();

    Process proc = new Process();
    proc.StartInfo.FileName = Environment.GetEnvironmentVariable("TMP") + filename;
    proc.StartInfo.CreateNoWindow = true;
    proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    proc.StartInfo.UseShellExecute = true;

    proc.Start();
    proc.PriorityClass = ProcessPriorityClass.Normal;
}

Back to C# Code Snippet List