R – What’s the best way for a .NET Compact Framework executable to delete itself

compact-frameworknetwindows-mobile

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.

Best Answer

In Windows Mobile there is the directory:

\application data\volatile

This directory is kept for as long as the device has power.

So what we do is:

  1. 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).
  2. run the exe from this directory.
  3. delete the files you want.
  4. (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.