Creating Another Self-destruct Program


You may find the title of this article somewhat misleading. Yes, I have written about this subject before, but as I have said many a time: There are many ways to skin a cat. What I will show you today will also make use of batch files to delete the program, but with one caveat. The program must first be closed.

Now, why should a program such as this exist?

Well, say for instance you have a allowed a user to use your application for a certain period of time. A trial application. When the trial expires, so can the program. This, however, doesn’t solve the issue of uninstalling the application. Not all applications get installed similarly.


Create a new C# or Visual Basic.NET Console application. After the application has loaded, add these namespaces.


using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading;


Imports System.IO
Imports System.Reflection
Imports System.Threading

The namespaces import the Reflection and threading and file classes so that we can utilize them throughout our code.

Add the next code for the Sub Main procedure:


   static void Main(string[] args)
      string strBatch = string.Empty;
      string strEXE = Assembly.GetExecutingAssembly()
         .CodeBase.Replace("", string.Empty).Replace("/", "\");

      strBatch += "@ECHO OFFn";
      strBatch += "ping > nuln";
      strBatch += "echo j | del /F ";
      strBatch += strEXE + "n";
      strBatch += "echo j | del DelApp.bat";
      File.WriteAllText("DelApp.bat", strBatch);




   Private Sub Main(ByVal args As String())

      Dim strBatch As String = String.Empty
      Dim strEXE As String = Assembly.GetExecutingAssembly() _
         .CodeBase.Replace("", String.Empty).Replace("/", "")

      strBatch += "@ECHO OFF" & vbLf
      strBatch += "ping > nul" & vbLf
      strBatch += "echo j | del /F "
      strBatch += strEXE & vbLf
      strBatch += "echo j | del DelApp.bat"
      File.WriteAllText("DelApp.bat", strBatch)

   End Sub

A batch file gets created. It checks to see if the application is still open. If it is not open, it executes the created batch file.

Add another way.


   static void SelfDestruct()
      string strBatch = "DelApp.bat";
      using (StreamWriter swBatch = File.AppendText(strBatch))
         swBatch.WriteLine("Tasklist /if "PID eq " +
            Process.GetCurrentProcess().Id.ToString() + "" |
            find ":"");
         swBatch.WriteLine("if Errorlevel 1 (");
         swBatch.WriteLine("  Timeout /T 1 /Nobreak");
         swBatch.WriteLine("  Goto Loop");
         swBatch.WriteLine("Del "" + (new FileInfo((new
            .LocalPath)).Name + """);

      Process.Start(new ProcessStartInfo() { Arguments = "/C " +
         strBatch + " & Del " + strBatch, WindowStyle =
         ProcessWindowStyle.Hidden, CreateNoWindow = true,
         FileName = "cmd.exe" });


   Private Sub SelfDestruct()

      Dim strBatch As String = "DelApp.bat"

      Using swBatch As StreamWriter = File.AppendText(strBatch)


         swBatch.WriteLine("Tasklist /if ""PID eq " & _
            Process.GetCurrentProcess().Id.ToString() & _
            """ | find "":""")
         swBatch.WriteLine("if Errorlevel 1 (")
         swBatch.WriteLine("  Timeout /T 1 /Nobreak")
         swBatch.WriteLine("  Goto Loop")
         swBatch.WriteLine("Del """ & (New FileInfo((New Uri _
            (Assembly.GetExecutingAssembly().CodeBase)) _
            .LocalPath)).Name & """")

      End Using

      Process.Start(New ProcessStartInfo() With {
             .Arguments = "/C " & strBatch & " & _
                Del " & strBatch,
             .WindowStyle = ProcessWindowStyle.Hidden,
             .CreateNoWindow = True,
             .FileName = "cmd.exe"

   End Sub

When you call this sub procedure, it keeps on checking the task list to see if the application is open or not. If it is no longer open, it deletes it.


It is not difficult creating a self-destructing program, but use this with caution. As outlined above, ensure that there are valid reasons for this. Until next time, happy coding and destructing!

Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Must Read