title: How can I make my .NET application erase itself?
body: <p>How can I make my C# app erase itself (self-destruct)? Here's two ways that I think might work:</p>
  
  <ul>
  <li>Supply another program that deletes the main program. How is this deleter program deleted then, though?</li>
  <li>Create a process to CMD that waits a few seconds then deletes your file. During those few seconds, you close your application.</li>
  </ul>
  
  <p>Both of those methods seem inefficient. I have a feeling that there's some built-in flag or something in Windows that allows for such stuff. How should I do it? Also, can you provide some sample code? </p>
  
  <p><strong>UPDATE:</strong> Thanks for all your answers! I'm going to try them, and see where that gets me.</p>
  
  <p>First of all, some people have asked why I'd want my app to do this. Here's the answer: a few days ago, I read the Project Aardvark spec that Joel Spolsky posted on his blog, and it mentioned that the client app would delete itself after the remote session. I'm wondering how this works, and how, if I ever need to do this, I can accomplish such a feat. </p>
  
  <p>Here's a little overview of what's been suggested:</p>
  
  <ul>
  <li><strong>Create a registry entry that tells Windows to delete the file on reboot</strong></li>
  <li><strong>Launch CMD with a ping command to wait a few seconds and then delete the file</strong></li>
  </ul>
  
  <p>Both of those, of course, have their disadvantages, as outlined in the comments.</p>
  
  <p>However, would such a method as outlined below work?</p>
  
  <p>There are two executables: Program.exe and Cleaner.exe. The former is the program itself, the latter is the app that deletes Program.exe and itself (if it's loaded into memory, as I'm about to explain). Is it possible for Program.exe (which has dependencies) to <strong>load</strong> all of Cleaner.exe, which doesn't have any dependencies, <strong>into memory</strong> and run it?</p>
  
  <p>If this is possible, could Cleaner.exe be packaged inside Program.exe, loaded into memory, and run?</p>