2

Hello fellow developers,

Right now, during one of my creative insomnia sessions I've been working on a project of mine while learning C# and I happened to wonder - is there a way to create a self-deleting application ?

Now, do not mistake me for someone who's too lazy to do his research. I've done some experimenting of my own (well, not much of experimenting really, as File. functionality did not seem to be capable of such a task - not surprising, windows is fairly reluctant when it comes to delete an application that's running).

I did a bit of research and turns out some of my fears were true - such functionality would need to either execute a command line process or the MOVEFILE_DELAY_UNTIL_REBOOT flag. The second option is absolutely out of question while the first one is still somewhat messy. Therefore I'm just curious - have any of the more experienced CSharp developers around here found another way to do it ? (Theoretically, it does sound impossible. But I would be foolish to underestimate fellow developers)

EvilBeer
  • 2,054
  • 2
  • 19
  • 37
  • why is first one 'messy'? – muratgu Sep 07 '13 at 02:01
  • So you want to delete your own application inside your own application . right ? – sudhansu63 Sep 07 '13 at 02:02
  • muratgu, Well, messy might not have been the best word to choose. Basically, I'd much rather avoid reaching out for the command line if that's possible in any way. sudhAnsu63, Precisely. As I already pointed out in the post, according to my understanding it should be impossible, although deep inside I still have faith that someone might have found a way. jwrush, it is not a duplicate. Please read the entire post instead of assuming things based on the name of the thread :) – EvilBeer Sep 07 '13 at 02:08
  • @Applebough The question is worded slightly different, but the answer is the same. – Ed Chapel Sep 07 '13 at 10:37

2 Answers2

1

Possible approach... Not tested...

There is another FILE_FLAG_DELETE_ON_CLOSE flag of CreateFile that you can consider to play with.

  • Create temporary file with this flag and copy your executable there.
  • Start second instance from that location and pass path to own location as parameter
  • After second instance started (need some synchronization or basic Sleep for learing purposes)
  • In new instance delete first file (may retry till first instance quits) and quit.too.

At that point second file should have no handles to it and will be deleted by OS too.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

Your question is already asked here.

You can do this by using MoveFileEx API, whichg gives you MOVEFILE_DELAY_UNTIL_REBOOT flag, which will delete what you want on next system startup.

Community
  • 1
  • 1
Alyafey
  • 1,455
  • 3
  • 15
  • 23