What's the best way for a .NET Compact Framework executable to delete itself?
I'd like my .NET Compact Framework app to delete itself after it finishes executing. What's the best way to do this?
On a Windows machine it's possible to do this using a .bat file, but that's not an option on mobile devices.
In Windows Mobile there is the directory:
This directory is kept for as long as the device has power.
So what we do is:
- copy a exe over to this directory, or a sub-directory (I normally use a sub-directory to make sure we don't clash with another application).
- run the exe from this directory.
- delete the files you want.
- (optional) soft-reset.
For compatiblity with all devices you should use the "SHGetSpecialFolderPath" function with CSIDL_APPDATA to get the application data directory and append "\volatile" to it.
One idea is you could use wceload:
Write a cab file that has a custom step (I think you might need to create a setup.dll) that will delete your application.
Get your application to call wceload when it exits to run this cab, passing the argument for wce load to delete the cab after it's finished running.
Not very slick I know.
Write your own deleter.exe program and run that to delete your main exe, but you'd have to leave that behind on the device... unless your device has a ram drive you could run deleter.exe from, so that it would disappear on reboot.