|
-
March 5th, 2005, 05:24 AM
#1
Delete file at startup or shutdown
Hi,
What I need to do is schedule some files to be deleted either at shutdown or at startup. My application can't do this because it is closing after it creates them and they need to remain there until shutdown. My app is currentlly creatting bat files for each of them and adds them to Startup folder in Start Menu. This method is inneficient though. For example how do uninstallers delete their executable at shutdown (startup)???
Thanks,
Andrew
-
March 5th, 2005, 05:40 AM
#2
Re: Delete file at startup or shutdown
Take a look in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run . All application register there are launched when Windows starts up. You can write a small program to delete the file and register it there.
About shutdown, you can write a program that handles WM_QUERYENDSESSION and delees whatever you need. If you are interested in writing a service, it receives SERVICE_CONTROL_SHUTDOWN when system shuts down, if registered for SERVICE_ACCEPT_SHUTDOWN.
-
March 5th, 2005, 07:25 AM
#3
Re: Delete file at startup or shutdown
Or,
You could create a small script that does your file operations and then
you can use control panel->scheduled tasks->Add scheduled task to add this as a recurring task.
Although it is cleaner and scaleable, it is annoying that the task scheduler gives you an option to run tasks when computer starts, but no choice for doing tasks just before computer shuts down...
But anyways as you want "either" at startup or shutdown, this approach should work well for you.
hth
-Vinayak
-
March 5th, 2005, 11:04 AM
#4
Re: Delete file at startup or shutdown
Hi,
First let me thank you both for your responces.
Cilu I can't write another app to delete them due to project specifications. If I would ahve been alowed to do that I would have done from the begining.
And I can't just add a schedule from control panel because I have to do it all through code. I still wonder how uninstallers delete their own exe after uninstall???
Thanks.
-
March 5th, 2005, 11:27 AM
#5
Re: Delete file at startup or shutdown
I still wonder how uninstallers delete their own exe after uninstall???
When you install a program a configuration file is written with the files, registry keys, etc. created during installation, and this is used to uninstall. These are not deleted when the system shuts down, but the moment you run the "uninstallation".
-
March 5th, 2005, 11:45 AM
#6
Re: Delete file at startup or shutdown
Hi,
No I don't think you understood me right. I meant the exe (like uninstall.exe) that performs the removal of files needed to be deleted. It can't delete itself cause it is running, so how does it delete itself after he deletes the other files?
-
March 5th, 2005, 11:49 AM
#7
Re: Delete file at startup or shutdown
Use MoveFileEx with the MOVEFILE_DELAY_UNTIL_REBOOT and a null destination. There are some restrictions so read the link.
You can add tasks programatically via the ITask interfaces
-
March 5th, 2005, 12:18 PM
#8
Re: Delete file at startup or shutdown
 Originally Posted by nemok
No I don't think you understood me right. I meant the exe (like uninstall.exe) that performs the removal of files needed to be deleted. It can't delete itself cause it is running, so how does it delete itself after he deletes the other files?
Most apps don't have an Uninstall.exe (or equivalent). Their Setup.exe programs use a commion installer/uninstaller built into Windows. The Windows installer carries out tasks such as keeping a reference count of which applications need a particular DLL. Then, if you uninstall one of the apps, the DLL gets left behind if there are still other apps installed that need it. So "Windows Installer" really takes care of the uninstall in the majority of cases.
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
March 5th, 2005, 12:23 PM
#9
Re: Delete file at startup or shutdown
Yes but what about the others like installers made with NSIS from Nullsoft? Do they use MoveFileEx ??? Because I noticed that for some the uninstall exe remains untill reboot.
-
March 5th, 2005, 03:28 PM
#10
Re: Delete file at startup or shutdown
Are you asking about startup and shutdown or a self deleting exe? [I see I missed your 11:45 response] If it is the former then MoveFileEx(...) and the ITask should be sufficent. If it is the latter then there are a number of ways to do it.
Bat filess, injecting code, to using the native api to duplicate process creation and setting the delete on close flag [I have to dig into some old code and see if that would actually work...]
Let google be your friend..do a google on FILE_FLAG_DELETE_ON_CLOSE I am sure there are plenty of discussions on it.
Older article
http://www.microsoft.com/msj/archive/SF9C.aspx
Last edited by Mick; March 5th, 2005 at 03:34 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|