[RESOLVED] Killing a file
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: [RESOLVED] Killing a file

  1. #1
    Join Date
    Jul 2002
    Location
    Piscataway, NJ, USA
    Posts
    23

    [RESOLVED] Killing a file

    Hi,

    My question is about deleting a temp pst file that is created by our code in Vista environment.

    Code makes a copy of the system user profile pst with a different name, uses that file to open other emails and attachments for us to extract. After we are done, we delete the temporary file using kill filename.

    Although this works fin in XP, somehow vista is holding on to the file longer time while code is running. If I debug it, kill function works, although still need to wait a second or two.

    How can release that pst file and make sure that I can delete it in the code? Is there such a command or tricks to it?

    Thanks
    Logic is the future...

    leokoach.com

  2. #2
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,782

    Re: Killing a file

    You could use a timer and an error handler put the code to kill the file along with an error handler in the timer tick routine. Activate the timer when you are ready to kill the file and place code in the timer tick routine to disable the timer once it has actually killed the file. This way it will keep trying until the file is deleted.

  3. #3
    Join Date
    Jul 2002
    Location
    Piscataway, NJ, USA
    Posts
    23

    Re: Killing a file

    Actually I do have a routine with a function called forcetokill. Interesting thing is, even if I put a loop with a timer, it loops forever. If I stop it for debug, and continue a second or two later, it deletes the file. This works in XP but not in Vista. Something is keeping the handle ties while code is running. I wonder if Vista kernel has something to do with it.
    Logic is the future...

    leokoach.com

  4. #4
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,874

    Re: Killing a file

    Create a SECOND program called ForceKill() that waits while you close your app, then if the pst file isn't release, to kill it after your program finishes

    Also, what folder is it written in? Try a subfolder somewhere else
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Sep 2001
    Location
    South Africa
    Posts
    186

    Re: Killing a file

    You should give the OS time to actually finish the kill e.g.
    Code:
    Kill "Your temp file.tmp"
    DoEvents 
    Sleep(1000) 'wait a second or so, 1000 milliseconds = 1 second
    Maybe this will help.

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,074

    Re: Killing a file

    Just a question LeoKoach..
    Is the UAC turned on on the Vista machine ¿

  7. #7
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,782

    Re: Killing a file

    Quote Originally Posted by LeoKoach View Post
    Actually I do have a routine with a function called forcetokill. Interesting thing is, even if I put a loop with a timer, it loops forever. If I stop it for debug, and continue a second or two later, it deletes the file. This works in XP but not in Vista. Something is keeping the handle ties while code is running. I wonder if Vista kernel has something to do with it.
    Don't use a loop. Use a timer control. Unless of course you need the file deleted before you continue on to the next line of code in which case the sleep statement suggested by another poster within a loop may do the trick though you should use a timeout in the loop so that it will exit at some point if the file is not deleted after a period of time.

    In general I would avoid long sleep statements as they make the program non-responsive, A timer will work with the program and cause no noticable performance hit.

  8. #8
    Join Date
    Jul 2002
    Location
    Piscataway, NJ, USA
    Posts
    23

    Re: Killing a file (Workaround)

    UAC is off. I used “timer” and “sleep” in the loop and without loop. Unless if I debug, or close the IDE, I can't delete the file. When I run the same code over and over again, it's random that file gets deleted.

    I worked this thing around for now

    Each time program runs that code, it creates a session. Each session number is unique and it also creates temporary pst file such as 0001.pst 0002.pst etc. What I did is, anytime program returns "0" error code when it tries to delete, I assume I can delete all the leftover pst files and I just kill everything in the batch folder with pst extension.

    This seems like working fine. Leftovers are not harmful, although this is only a workaround, not the fix... I have to live with it for while

    Thanks all for your replies, if you have any other solutions, please let me know.

    Leo
    Logic is the future...

    leokoach.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center