Mega Code Archive

 
Categories / Delphi / Examples
 

How to delete our own application. (beta 2, 3 and 4)

Title: How to delete our own application. (beta 2, 3 and 4) Question: In article 514 (http://www.delphi3000.com/articles/article_514.asp), the main question was to delete the own application after exit. The resut was reached by using a Batch file. I think that's not the better way. Here are 2 solution in how doing that. Answer: ========================= BETA 2 ========================= This solution comes from my idea of doing a installer program. InstallShield(C) and others just uses that solution. Do not use a Batch file, instead use a small (about 25K) console application that just takes one parameter (the file that must be deleted) and waits until your application unloads and/or EXE file is unlocked. Just run it from your application before exiting. Program DelFile; {$APPTYPE CONSOLE} Uses SysUtils, Windows; Begin If ( ParamCount = 0 ) Then Exit; Repeat Sleep( 10 ); Until ( DeleteFile( PChar( ParamStr( 1 ) ) ) ); End. To do a better work, the DelFile.Exe file should be put during the installation of the program into the Windows\Temp folder and runned from there, specifing the complete path of the file that must be deleted. PRO: The batch file can be modified and can be done to not delete your file. VS: You must hide a program in the Windows\Temp folder of a user, that's not good. ========================= BETA 3 ========================= You can use the registry key: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Runonce to add a command that will be runned only once, and your application will be deleted next type your Windows will be restarted. Key name: "DeleteMyOwnApplicationFile" Key value: "c:\windows\temp\delfile.exe c:\myapp\myapp.exe" PRO: The next time Windows starts, you application will be surelly not loaded, also if it's in the Statup folder. VS: You must hide a program in the Windows\Temp folder of a user, that's not good. The system must be rebooted. ========================= BETA 4 ========================= Always using, the key registry, don't use your delfile.exe, but: Key name: "DeleteMyOwnApplicationFile" Key value: "del c:\myapp\myapp.exe" ...using the DOS "Del" command. PRO: You don't need to hide programs. VS: The system must be restarted.