CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2003
    Location
    Bacau, Romania
    Posts
    474

    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
    B.A. (NKProds)

    Here is what I do: http://www.nkprods.com
    I couldn't have done it without your help. Thanks.

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Nov 2004
    Location
    Virginia, The lovers' state
    Posts
    64

    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

  4. #4
    Join Date
    Nov 2003
    Location
    Bacau, Romania
    Posts
    474

    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.
    B.A. (NKProds)

    Here is what I do: http://www.nkprods.com
    I couldn't have done it without your help. Thanks.

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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".
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Nov 2003
    Location
    Bacau, Romania
    Posts
    474

    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?
    B.A. (NKProds)

    Here is what I do: http://www.nkprods.com
    I couldn't have done it without your help. Thanks.

  7. #7
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537

    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

  8. #8
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867

    Re: Delete file at startup or shutdown

    Quote 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

  9. #9
    Join Date
    Nov 2003
    Location
    Bacau, Romania
    Posts
    474

    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.
    B.A. (NKProds)

    Here is what I do: http://www.nkprods.com
    I couldn't have done it without your help. Thanks.

  10. #10
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537

    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
  •  





Click Here to Expand Forum to Full Width

Featured