Link to home
Start Free TrialLog in
Avatar of iboutchkine
iboutchkine

asked on

Wait until file is copied

I need to copy the file and then do something with it.

I am using
File.Copy(Source, Destiination)

If I strat doing something with the file while it is still in the process of being copied I am getting the exception that Access to the file denied.
Does  somebody have a solution how to make sure that the file has been successfully copied and Copying process is finished?

I was trying to check if the file size of the Destination is equal to the size of Source

Dim fi As FileInfo
Dim lLengthOfOld As Long

            'get the length of the file
            fi = New FileInfo(Source)
            lLengthOfOld = fi.Length

            File.Copy(Source, Destination)
Label1:
                Try
                    fi = New FileInfo(Destination)
                    'wait until copied
                    Do While Not fi.Length = lLengthOfOld
                       fi = New FileInfo(Destination)
                        Application.DoEvents()
                    Loop
                Catch
                    GoTo Label1
                End Try


But sometimes I still get the message that Access denied.
If I run code in Debug mode, execute 1 line at a time, everything works OK
Avatar of Mikal613
Mikal613
Flag of United States of America image

use sleep

http://www.codeproject.com/useritems/The_UDP_under_VBNET.asp

depending on how big the file is

seconds of waiting
Avatar of iboutchkine
iboutchkine

ASKER

I have already tried with sleep. It is working, but the problem is that it always takes different time to copy the file. It depends on the type of PC and how PC is busy with other processes. That is why it is not an option. The bottom line - I want to start synchronous process of copying a file and continue only after the copy is done. So far I cannot catch the end of the process. Even if I manage to compare bits written (like in the sample above) it does not mean that the file is closed.
so do a Do while statement

While you get the error of access denied
SOLUTION
Avatar of AdsB
AdsB

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Mikal613
After copying the file I start external program which generates an error - Access denied. My program runs on the background (without user presence). That is why I cannot afford to have messages to appear on my screen

 AdsB
Can you please give me a code

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial


The only down side to the code provided is that your file must be less than 2.1 Gig in size, which woulnd't work if you are creating a back up program of some sort.


Thank you _TAD_ I will try and get back to you.
Thank you _TAD_ and AdsB. I got it to work. Her eis the code for whoever is interested

 Dim i As Integer
        Dim sFile As String
        Dim arrFiles() As String
        Dim posSep As Integer
        Dim br As BinaryReader
        Dim bw As BinaryWriter


        arrFiles = GetFiles(sDir) 'from MailBox directory

        For i = 0 To arrFiles.GetUpperBound(0)
            posSep = arrFiles(i).LastIndexOf("\")
            ' Get the file name from the full path
            sFile = arrFiles(i).Substring((posSep + 1), arrFiles(i).Length - (posSep + 1))

            If sFile.Substring(0, 3) = "ib_" Then
                br = New BinaryReader(New FileStream(sDir & sFile, FileMode.Open))
                bw = New BinaryWriter(New FileStream(OUT & sFile, FileMode.Create))
                bw.Write(br.ReadBytes(CInt(br.BaseStream.Length)))
                bw.BaseStream.Flush()
                br.BaseStream.Close()
                bw.BaseStream.Close()
            End If
        Next i