G
Guest
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
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