views:

519

answers:

4

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..

+4  A: 

Probably this can help you:

http://www.codeproject.com/KB/threads/HowToDeleteCurrentProcess.aspx

Another link that seems better

Aamir
Wow. That second link looks very comprehensive. Thanks for that. +1
peSHIr
+1 for the second link
roe
+4  A: 

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.

peSHIr
This is actually a common problem when writing said uninstaller.. it should remove the program, along with itself.
roe
Which is not a problem when using an installer subsystem that is part of the OS you're on, which is what I menat (implicitly, I admit). I'll edit my reply to make that clearer.
peSHIr
Thank You peSHlr Good idea.........
Sakthikannan
+1  A: 

If your goal is to run your EXE and then have it removed sooner-or-later, there are a couple of options:

1) MoveFileEx() API

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

JBRWilkinson
A: 

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

munissor