SoFunction
Updated on 2025-03-06

Implementation method of self-deletion of program in C#

C# program self-delete

The core implementation method is to call cmd to pass it into the command line, wait for a few seconds before deleting the file;

The exe file cannot be deleted when the application is running. But exe can be renamed and files can be moved within the drive;

Deleting an application allows cmd to be deleted. In cmd, you can use the timeout command to delay execution of the commands, and then execute subsequent logic through && to achieve delayed execution of the commands.

Let cmd delay executing the DEL command to delete the application. After the application is called to delete, the application can be finished.

The code is as follows

static void Main(string[] args)
{
     var fileName = ().;
     DelayDeleteFile(fileName, 2);    //This is to delete the program 2 seconds after closing the program}

private static void DelayDeleteFile(string fileName, int delaySecond = 2)
{
     fileName = (fileName);
     var folder = (fileName);
     var currentProcessFileName = (fileName);

     var arguments = $"/c timeout /t {delaySecond} && DEL /f {currentProcessFileName} ";

     var processStartInfo = new ProcessStartInfo()
     {
          Verb = "runas", // If the program has administrator permissions, then running cmd is also administrator permissions          FileName = "cmd",
          UseShellExecute = false,
          CreateNoWindow = true, // If you need to hide the window, set to true and do not display the window          Arguments = arguments,
          WorkingDirectory = folder,
     };

     (processStartInfo);
}

Winform usage example

static void Main()
        {
            ();
            (false);
            (new Form1());

            var fileName = ().;
            DelayDeleteFile(fileName, 2);

        }
        private static void DelayDeleteFile(string fileName, int delaySecond = 2)
        {
            fileName = (fileName);
            var folder = (fileName);
            var currentProcessFileName = (fileName);

            var arguments = $"/c timeout /t {delaySecond} && DEL /f {currentProcessFileName} ";

            var processStartInfo = new ProcessStartInfo()
            {
                Verb = "runas", // If the program has administrator permissions, then running cmd is also administrator permissions                FileName = "cmd",
                UseShellExecute = false,
                CreateNoWindow = true, // If you need to hide the window, set to true and do not display the window                Arguments = arguments,
                WorkingDirectory = folder,
            };

            (processStartInfo);
        }

WPF usage example

First add ShutdownMode="OnExplicitShutdown" in it and delete StartupUri=""

Then add the following code:

protected override void OnStartup(StartupEventArgs e)
        {
            (e);
            new MainWindow().ShowDialog();

            var fileName = ().;
            DelayDeleteFile(fileName, 2);

            ();
        }

        private static void DelayDeleteFile(string fileName, int delaySecond = 2)
        {
            fileName = (fileName);
            var folder = (fileName);
            var currentProcessFileName = (fileName);

            var arguments = $"/c timeout /t {delaySecond} && DEL /f {currentProcessFileName} ";

            var processStartInfo = new ProcessStartInfo()
            {
                Verb = "runas", // If the program has administrator permissions, then running cmd is also administrator permissions                FileName = "cmd",
                UseShellExecute = false,
                CreateNoWindow = true, // If you need to hide the window, set to true and do not display the window                Arguments = arguments,
                WorkingDirectory = folder,
            };

            (processStartInfo);
        }

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.