PC Review


Reply
Thread Tools Rate Thread

Checking that a file is not in the process of being copied

 
 
=?Utf-8?B?Q3JhaWcgSEI=?=
Guest
Posts: n/a
 
      28th Nov 2005
How can I check that a file is not in the process of being copied ?

I want to copy a file from a source path to a destination path, but if the
file is in the process of being copied to the source path, then my code will
fail. If it is in the process of being copied then my code needs to wait for
the copying to finish.

The code below is what I tried, but file.length returns the full file
length, even when the whole file hasn't finished copying.

Any ideas ?
Craig



Private Shared Sub CheckFileCopyComplete(ByVal filePath As String)

Const MAX_TIME_IN_SEC As Integer = 60 * 5 '5 mins
Const PAUSE_IN_MILLISEC As Integer = 500 '0.5 sec

Dim file As New IO.FileInfo(filePath)
Dim fileLength As Long = 0
Dim startTime As DateTime = System.DateTime.Now

Do Until file.Length = fileLength
fileLength = file.Length
System.Threading.Thread.Sleep(PAUSE_IN_MILLISEC)
If System.DateTime.Now.Subtract(startTime).Seconds > MAX_TIME_IN_SEC
Then
Throw New ApplicationException("Timeout waiting for file to copy")
End If
Loop

'file finished copying: move on

End Sub
 
Reply With Quote
 
 
 
 
Gabriel Lozano-Morán
Guest
Posts: n/a
 
      28th Nov 2005
FileSystemWatcher OnCreated() or Created event will be triggered when the
file is copied:
http://msdn.microsoft.com/library/de...eatedtopic.asp

Gabriel Lozano-Morán


 
Reply With Quote
 
=?Utf-8?B?Q3JhaWcgSEI=?=
Guest
Posts: n/a
 
      28th Nov 2005
Thanks, but I do not want to be alerted when a file has been created, but
just check that a file is not in the process of being copied before I perform
some operation on it.

 
Reply With Quote
 
Patrice
Guest
Posts: n/a
 
      28th Nov 2005
Use try catch to catch the error ?

--
Patrice

"Craig HB" <(E-Mail Removed)> a écrit dans le message de
news:854461D2-A9FE-45D7-A5E0-(E-Mail Removed)...
> How can I check that a file is not in the process of being copied ?
>
> I want to copy a file from a source path to a destination path, but if the
> file is in the process of being copied to the source path, then my code

will
> fail. If it is in the process of being copied then my code needs to wait

for
> the copying to finish.
>
> The code below is what I tried, but file.length returns the full file
> length, even when the whole file hasn't finished copying.
>
> Any ideas ?
> Craig
>
>
>
> Private Shared Sub CheckFileCopyComplete(ByVal filePath As String)
>
> Const MAX_TIME_IN_SEC As Integer = 60 * 5 '5 mins
> Const PAUSE_IN_MILLISEC As Integer = 500 '0.5 sec
>
> Dim file As New IO.FileInfo(filePath)
> Dim fileLength As Long = 0
> Dim startTime As DateTime = System.DateTime.Now
>
> Do Until file.Length = fileLength
> fileLength = file.Length
> System.Threading.Thread.Sleep(PAUSE_IN_MILLISEC)
> If System.DateTime.Now.Subtract(startTime).Seconds >

MAX_TIME_IN_SEC
> Then
> Throw New ApplicationException("Timeout waiting for file to

copy")
> End If
> Loop
>
> 'file finished copying: move on
>
> End Sub



 
Reply With Quote
 
=?Utf-8?B?Q3JhaWcgSEI=?=
Guest
Posts: n/a
 
      28th Nov 2005
yes, i think i'll do that -- thanks
 
Reply With Quote
 
=?Utf-8?B?Q3JhaWcgSEI=?=
Guest
Posts: n/a
 
      29th Nov 2005
I decided to catch the exception, pause and then try again (as suggested),
but it meant searching through the ex.message for "because it is being used
by another process".

I have copied the procedure below. If anyone have a neater way to do this,
please let me know.

Thanks,
Craig


Private Sub CopyFileWhenReady(ByVal source As String, ByVal dest As String)

Const MAX_TIME_IN_SEC As Integer = 60 * 5 '5 mins
Const PAUSE_IN_MILLISEC As Integer = 500 '0.5 sec
Const ERROR_MSG_EXRACT As String = "because it is being used by another
process"

Dim copiedOk As Boolean = False
Dim startTime As DateTime = System.DateTime.Now

Do Until copiedOk
Try
IO.File.Copy(source, dest)
copiedOk = True
Catch ex As IO.IOException
If ex.Message.IndexOf(ERROR_MSG_EXRACT) > -1 Then
System.Threading.Thread.Sleep(PAUSE_IN_MILLISEC)
If System.DateTime.Now.Subtract(startTime).Seconds > MAX_TIME_IN_SEC
Then
Throw New ApplicationException("Timeout waiting for file to copy")
End If
Else
Throw ex
End If
End Try
Loop

End Sub
 
Reply With Quote
 
Patrice
Guest
Posts: n/a
 
      29th Nov 2005
Try to see the documentation for the exact exception you should catch. In
most cases you'll find additional members (I believe this is ErrorCode or
HResult or something similar) that gives some additional information about
the problem...

The text could perhaps change and is likely localized either depending on
the os or the language pack...

You could also try perhaps to open the file in exclusive mode before doing
the copy...

--
Patrice

"Craig HB" <(E-Mail Removed)> a écrit dans le message de
news:23134C67-3A13-41BF-BDE8-(E-Mail Removed)...
> I decided to catch the exception, pause and then try again (as suggested),
> but it meant searching through the ex.message for "because it is being

used
> by another process".
>
> I have copied the procedure below. If anyone have a neater way to do this,
> please let me know.
>
> Thanks,
> Craig
>
>
> Private Sub CopyFileWhenReady(ByVal source As String, ByVal dest As

String)
>
> Const MAX_TIME_IN_SEC As Integer = 60 * 5 '5 mins
> Const PAUSE_IN_MILLISEC As Integer = 500 '0.5 sec
> Const ERROR_MSG_EXRACT As String = "because it is being used by another
> process"
>
> Dim copiedOk As Boolean = False
> Dim startTime As DateTime = System.DateTime.Now
>
> Do Until copiedOk
> Try
> IO.File.Copy(source, dest)
> copiedOk = True
> Catch ex As IO.IOException
> If ex.Message.IndexOf(ERROR_MSG_EXRACT) > -1 Then
> System.Threading.Thread.Sleep(PAUSE_IN_MILLISEC)
> If System.DateTime.Now.Subtract(startTime).Seconds > MAX_TIME_IN_SEC
> Then
> Throw New ApplicationException("Timeout waiting for file to

copy")
> End If
> Else
> Throw ex
> End If
> End Try
> Loop
>
> End Sub



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Checking whether a process is in execution =?Utf-8?B?a2Q=?= Microsoft VB .NET 4 6th Mar 2005 08:39 AM
Conflicting used space information when checking the drive property against marking all files and checking the marked file properties. elloko Windows XP Configuration 3 19th Dec 2004 05:34 AM
checking if file is in use by another process sidd Microsoft C# .NET 2 17th Sep 2004 09:16 AM
checking if file is in use by another process sidd Microsoft Dot NET 0 15th Sep 2004 05:07 AM
Copied my hard drive, now can't boot, License checking problem =?Utf-8?B?QWwgU2ltbW9ucw==?= Windows XP Help 2 23rd Dec 2003 09:21 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:18 PM.