
Creating a self deleting executable using .NET
Quote:
>I am trying to create an application for the Windows Mobile 5.0 (or higher)
> platform. One of the features of the application is that it has a panic
> button that automatically closes down the application and deletes the
> executable.
> I have been trying to find a technical solution for this feature but it
> has
> proven to be much harder than I thought. The problem is that the physical
> executable file is locked and can't be deleted as long as the executable
> is
> running.
> I found some quite interesting articles explaining the problem in more
> detail and also explaining some possible solutions. An example of a very
> usefull article on this subject can be found on:
> http://www.catch22.net/tuts/selfdel
> Unfortunately the solutions in the article require C++ and I am trying to
> achieve a solution that will work in VB.NET or C#.NET. Is there anybody
> who
> can help me a bit further with this problem ? Any help is appreciated.
Well, you're not going to be able to have the application delete itself, as
you know, which means that some other applicaiton is going to have to do it.
I would create a "launch" application that is the one that actually runs
when the program is started. This application would spawn a new process,
which then executes your "main" application in that new process.
When your panic button is pressed, it is the "launch" application that
handles it by shutting down the secondary process (the one running your main
application) and then deleting the file (which you could now do because the
application is no longer running).
Here's some code to get you started:
Sub Main()
'Launch main application in a new process...
If System.IO.File.Exists("path to main application here") Then
mainAppProc = System.Diagnostics.Process.Start("path to main
application here")
'If you don't set this, you won't get event notifications
because the events won't be raised.
mainAppProc.EnableRaisingEvents = True
End If
End Sub
Sub PanicButton_Click() Handles btnPanic.Click
'Stop the process...
'Killing a process can have serious ramifications!
If Not mainAppProc .HasExited Then
calcProc.Kill()
End If
'Delete the main application
Dim FileToDelete As String
FileToDelete = "C:\testDelete.txt"
If System.IO.File.Exists(FileToDelete) Then
System.IO.File.Delete(FileToDelete)
'Shut down the launch application
Application.Quit
En Sub
In the end, the main application's assembly should be shut down and deleted,
but you will still have the "launch" application resident on the device.
-Scott