|
-
February 6th, 2006, 08:29 AM
#1
How to find if the file is busy
I need to copy the file (OutboxLog.txt produced by Fax service) right after it was updated. I'm using:
System.IO.File.Copy(strSource, strDestination, True) to do that, but if the source file is busy (was not closed yet after update) the copy process fails.
How can I find if the file is still busy?
Thank you
Vlad
-
February 6th, 2006, 08:51 AM
#2
Re: How to find if the file is busy
When you do the copy, surround it with a Try...Catch block. If failed you will be able to see it in the exception message.
-
February 6th, 2006, 08:59 AM
#3
Re: How to find if the file is busy
Thank you.
I do not need a message. I need to copy file in any case, but I don't know when it will be not busy.
As a temporary solution I tried a loop:
For lngAttempt = 0 To 100000
Application.DoEvents()
Next
And it works. But I do not like this approach. I would prefer something like:
Do While IsBusy = True
IsBusy = some code telling the status of the file
Loop
Vlad
-
February 6th, 2006, 09:26 AM
#4
Re: How to find if the file is busy
I got a solution on another forum from Terry Olsen:
Do
Try
file.copy(source,dest)
Exit Do
Catch
End Try
Loop
It works!!!!
-
February 10th, 2006, 10:52 AM
#5
Re: How to find if the file is busy
After testing this approach within several days I realized that it doesn't work sometimes. Why, I have no idea. It just doesn't make any copy.
Could anybody suggest anything for either copying the file or reading its contents when the file is in use?
Thank you
Vlad
-
February 10th, 2006, 04:50 PM
#6
Re: How to find if the file is busy
Try something along these lines:
Dim StillTrying as boolean = True
Do While StillTrying
Try
File.Copy(Source, Dest)
StillTrying = False
Catch Ex as Exception
System.Threading.Thread.CurrentThread.Sleep(SomeNumberOfMilliseconds)
End Try
Loop
You might also want to add a counter for failed attempts and break the loop after ?? tries.
-
February 10th, 2006, 05:07 PM
#7
Re: How to find if the file is busy
What I do normally is this. I start a timer and set a boolean variable to false at the start of file update operation. After it has done updating and the source file has been closed, I set the boolean variable to false. Within the timer_tick sub, I check if the boolean variable; if it is true, I give additional second or two (using sleep command), then I copy the updated file to the dest file. Good luck.
-
February 10th, 2006, 05:15 PM
#8
Re: How to find if the file is busy
Another method I have used successfully is this: I start a timer when updating has started. Within the timer_tick routine, I check if a dummy file exists at every tick. At the end of file update, I create the dummy file and put one dummy line of info in it, save and close it. When the timer routine sees the file (if file.exists(dummy.dat) then...), the copy operation is started. After copy, dummy file is deleted, and timer stopped. Make sure you stop the timer. In the previous example, make sure you reset the boolean variable to false and stop timer after file.copy command. Good luck.
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
|