Hi, I need to write a program which deletes itself while running...How to do in vb.net and what is the concept to do it..
Probably this can help you:
http://www.codeproject.com/KB/threads/HowToDeleteCurrentProcess.aspx
To have a program literally delete itself without any trace you need to ask at least someone else to delete your (last) executable after it has shut down. This is because a running executable will be in use, and you'd get a sharing violation if you delete it from the code that is running.
You could use Windows for this, I think, using a registry setting of commandlines to run when Windows starts. You could put `delete [full path to your executable]' there. (Google for the correct registry setting if you really need this.)
Another good option seems to be the first link in the reply by Aamir
: just before exiting your executable you start another process that uses parts of Windows to wait a bit and then delete your executable.
Yet another option is to make sure you have a correct installer package (e.g. an MSI package) that does what you want using the installer subsystem (e.g. MS Installer) of the OS you're on (e.g. Windows). But this only works if your application has/needs a full installer at all.
It would work like this: when the user uninstalls, let your installer packages make sure the application executable is not currently running. It can show the user that it is and ask to close application before continuing, or it can terminate its process after a confirmation. After this it can delete all files that were installed by the package, including the executable.
In VB.NET/C# in VisualStudio you can use a Custom Installer class to add the "make sure my executable is not running" logic. Or use a third party installer tool to create the installer package that can do this for you automatically.
If your goal is to run your EXE and then have it removed sooner-or-later, there are a couple of options:
If you specify the "MOVEFILE_DELAY_UNTIL_REBOOT" flag, and rename from 'your.exe' to '', then Windows will delete it on the next reboot.
2) Schedule a clean-up operation
Using the command-line AT command, SchTasks.exe or the more sophisticated TaskManager API, you can schedule a task to run 'later' which does:
%SYSTEMROOT%\CMD.EXE /C RMDIR /S /Q
You can enable shadow copy in your AppDomain and let the runtime delete the cached copy of your program.
See http://blogs.msdn.com/junfeng/archive/2004/02/09/69919.aspx