views:

236

answers:

3

How to programatically self delete?

C# / .NetCF2 / Windows Mobile 6

Please, I don't want to discuss WHY to do it, I just need to know HOW to do it!

Important:

  • The "second application" approach is NOT an option. (Unless that second application can be "extracted" from running app, but I don't know how to do it!).

  • No problem in forced reboot, if windows do the trick at startup. (Is it possible? Nice! Show me how!).

  • Code samples are welcome.

+2  A: 

I've done this in the past by simply writing a batch file to the file system that will wait a few seconds and then delete the program. You then use Process.Start() to kick off the batch file and immediately call Environment.Exit(). You need to make sure that the batch file waits long enough for your program to close, or it won't work.

Joel Coehoorn
Except batch files aren't supported in Windows Mobile...And whatever file you write out wold then be present leaving breadcrumbs.
ctacke
+3  A: 

The only way I can think of offhand to delete yourself and leave no trace is to use something already present in the device- namely wceload (the CAB extractor). I'd create a simple CAB file with a custom installer DLL that does a wait and then the delete.

I'd then add the CAB to the app as an embedded resource. When you need to delete you

  1. extract the CAB to the file system
  2. execute wceload.exe with the CAB as a parameter and /noui (or /silent)
  3. Quit your application

The CAB then deletes your file (a named mutex could sync this better than just a Sleep call in the DLL). wceload automatically deletes the CAB (well depending on WinMo version, but there is a switch to force delete if necessary).

It's certainly a hack, but it would provide a "leave no trace" delete. Of course the CAB would probably have to clean it's own installation registry entries as well. Maybe you could just have the install return "failure" to prevent them from being written in the first place.

ctacke
A: 

Windows can delete files on startup. It can be done by calling MoveFileEx like:

MoveFileEx(szDstFile, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);

I'm not sure if that API is available in Mobile 6 or not. However, what it actually does is create a registry entry in HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations. The key is a REG_MULI_SZ and you just need to give it a value like "szDstFile\0\0". On reboot Windows deletes the file.

As for programmatically rebooting, have a look at this thread on SO.

Corin
Unfortunately not supported under CE.
ctacke