Click here to Skip to main content
6,594,432 members and growing! (16,172 online)
Email Password   helpLost your password?
General Programming » Threads, Processes & IPC » General     Beginner License: The Code Project Open License (CPOL)

How to make your application delete itself immediately

By alexdresko

A simple two line technique that can be used in just about any application
C#, VC9.0, Windows (Win2K, WinXP, Win2003, Vista), .NET (.NET 1.0, .NET 1.1, .NET 2.0, .NET 3.0, .NET 3.5), Win32, Win64, MFC, WinForms, Dev
Version:2 (See All)
Posted:2 Dec 2008
Views:21,080
Bookmarked:81 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
63 votes for this article.
Popularity: 6.49 Rating: 3.61 out of 5
11 votes, 17.5%
1
2 votes, 3.2%
2
13 votes, 20.6%
3
12 votes, 19.0%
4
25 votes, 39.7%
5

Introduction

I'm sure you've all said to yourself or someone at the office at one point or another, "<snort> You idiot. Don't you know a windows application can't delete itself? I bet you don't even know how to type high ASCII characters using the ALT key and the number pad, gahhhh.." 

Sure, there are ways to have a file delete itself on the next reboot... And you can even resort to an external program or batch file to do the work. But I just came up with a nifty way of doing it without leaving a visible trace that the application ever existed!

Background 

This was just one of those simple, random ideas that popped up into my head. Took all of about 5 minutes to whip up a quick, working demo application from conception, and the technique is applicable to almost any Windows based development platform/language. 

In fact, it's taking me longer to post this on CodeProject! But since I've never posted anything on code project, I thought it'd be a fun, simple exercise. :) 

Using the code 

Please note that I cannot possibly post the code for all supported languages/frameworks/platforms where this technique will work. As you will soon see, there are only two lines required to have a program delete itself without leaving a trace. These two lines will probably vary slightly in your development environment of choice, but it shouldn't take more than a minute or two to translate the code to something that works perfect for you.

And here are those two lines of code (as noted by readers, this version doesn't work on XP).  

Process.Start("cmd.exe", "/C choice /C Y /N /D Y /T 3 & Del " + Application.ExecutablePath);
Application.Exit();
		  

Process.Start(...) is a .NET command that launches an application with parameters. The first parameter is the application you want to launch. The second parameter is a string containing the parameters you want to pass to the application to launch. This command, by default, does not pause and wait for the launched application to finish before moving on to the next command. That next command just happens to be Application.Exit().  In .NET land, that causes the currently running application to completely shutdown. Meanwhile, our command window has popped up and is merely waiting 3 seconds for the calling application to fully close. Once that happens, the Del command comes into play and blows away the application just prior to the command window closing on its own!

The following is a description of what those parameters mean.  

  1. Cmd /C causes a command window to appear and run the command specified.. it then causes the window to close auotmatically.
  2. Choice /C Y /N /D Y /T 3 displays displays an empty, flashing prompt. However, the /T 3 means that the prompt will automatically select the default choice Y (/D Y) after 3 seconds. I didn’t know about the “choice” command until a few minutes before writing this article.
  3. & is used to chain multiple commands together on a single line in a batch file. I didn’t know about & until just now either.
  4. Del <Application.ExecutablePath>... Well, I’m sure you can imagine what that does. Everything after the & can be replaced with anything you want to happen after the three second delay. You could have the command delete every file within the directory...or maybe something a little more malicious? :) 

 Here's another flavor of the same thing that works on XP

Process.Start("cmd.exe", "/C ping 1.1.1.1 -n 1 -w 3000 > Nul & Del " + 
          Application.ExecutablePath); 
Application.Exit();

This is fairly similar to the previous version except it uses the ping command to do the dirty work. -n 1 tells the command to only ping one time and -w 3000 tells the command to wait 3 seconds before performing the ping.  > Nul basically just hides the output of the ping command.

That's all, folks! You can place this code anywhere in your application, including the OnClosed event of your WinForms application.

Points of Interest

Several people have pointed out that this won't work if, for some reason, your application takes longer to shut down than what the timeout period is configured to. If you need a more fool proof technique, you might consider extending the timeout period and/or using the "delete on reboot" registry value.

Also, Filip Geens included code to hide the command prompt window from view...

ProcessStartInfo Info=new ProcessStartInfo();
Info.Arguments="/C choice /C Y /N /D Y /T 3 & Del "+
               Application.ExecutablePath;
Info.WindowStyle=ProcessWindowStyle.Hidden;
Info.CreateNoWindow=true;
Info.FileName="cmd.exe";
Process.Start(Info); 

If you find this code interesting, or if you know of a different technique that has the same benefits as this one, please let me know! Also, if you port this code to another language/platform, please post your version in the comments for others to benefit from. 

History

12/2/2008 10:29:11 PM:  Finished with version 1.. yee haw!  

12/3/2008 10:02:51 AM:  Added a version that works with XP and implemented some of the comments from readers. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

alexdresko


Member

Location: United States United States

Other popular Threads, Processes & IPC articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 53 (Total in Forum: 53) (Refresh)FirstPrevNext
GeneralHow to make your application delete itself immediately Pinmemberjonasas7:23 7 Aug '09  
GeneralMy vote of 1 PinmemberFahad Sadah7:16 9 Dec '08  
GeneralRe: My vote of 1 Pinmemberjgauffin3:42 4 Nov '09  
GeneralMy vote of 1 PinmemberHans-Werner Dietz1:42 9 Dec '08  
GeneralRe: My vote of 1 Pinmemberjgauffin3:43 4 Nov '09  
GeneralMy vote of 1 PinmemberSDX200022:48 8 Dec '08  
GeneralRe:How to make your application delete itself immediately PinmemberNaveen17:53 8 Dec '08  
GeneralI think this has a genuine use PinmemberPoiter113:27 8 Dec '08  
GeneralRe: I think this has a genuine use PinmemberSDX200022:51 8 Dec '08  
Generalself deleting batch file PinmemberCannibalSmith10:53 8 Dec '08  
GeneralMy vote of 1 Pinmemberryo-oh-ki8:51 8 Dec '08  
GeneralMy vote of 2 PinmemberNick Z.8:32 8 Dec '08  
General3 points <- ma vote PinmemberMember 337993412:28 7 Dec '08  
GeneralAnother way to do it, no matter if file is open PinmemberFederico Colombo14:35 5 Dec '08  
GeneralMy vote of 1 PinmemberJörg Metzler11:51 5 Dec '08  
GeneralMy vote of 1 PinmemberBooGhost10:44 5 Dec '08  
GeneralMy vote of 2 PinmemberDiego Mijelshon11:00 4 Dec '08  
GeneralGood Alternate Pinmemberashu fouzdar23:38 3 Dec '08  
GeneralNice Article.. PinsupporterBob Housedorf18:47 3 Dec '08  
GeneralNot a bad concept for an article... Pinmembersupercat912:57 3 Dec '08  
GeneralMy vote of 1 Pinmemberadahnert11:18 3 Dec '08  
GeneralRe: My vote of 1 Pinmemberalexdresko10:16 4 Dec '08  
GeneralRe: My vote of 1 PinmemberSDX200022:43 8 Dec '08  
GeneralMy vote of 1 [modified] PinmemberPanic2k310:40 3 Dec '08  
RantRe: My vote of 1 Pinmemberalexdresko10:15 4 Dec '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 2 Dec 2008
Editor: Chris Maunder
Copyright 2008 by alexdresko
Everything else Copyright © CodeProject, 1999-2009
Web12 | Advertise on the Code Project