Solved

Wait until file is copied

Posted on 2003-12-11
9
4,517 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:iboutchkine
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 9922252
use sleep

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

depending on how big the file is

seconds of waiting
0
 
LVL 28

Author Comment

by:iboutchkine
ID: 9922309
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.
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 9922337
so do a Do while statement

While you get the error of access denied
0
 
LVL 4

Assisted Solution

by:AdsB
AdsB earned 100 total points
ID: 9922405
Perhaps this problem goes away if you use an alternative to file.copy
Try copying the file under program control, by opeining it as a binary input file and copying it to an output file one buffer at a time.  It might take a little longer that way but not very much longer because the bottleneck is still really the disk speed not the extra CPU cycles needed to runb your code.  The real difference is that you will be in control of the process and will know exactly when it has finished.

I can give you sample code for this if you need it.

AdsB
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 28

Author Comment

by:iboutchkine
ID: 9922460
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

0
 
LVL 22

Accepted Solution

by:
_TAD_ earned 150 total points
ID: 9922518


This will copy a file very fast

I think it is similar to what ADSb suggested


                  BinaryReader br = new BinaryReader(new FileStream(@"C:\temp\test.txt",FileMode.Open));
                  BinaryWriter bw = new BinaryWriter(new FileStream(@"C:\test.txt",FileMode.Create));
                  bw.Write(br.ReadBytes((int)br.BaseStream.Length));
                  bw.BaseStream.Flush();
                  br.BaseStream.Close();
                  bw.BaseStream.Close();
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 9922535


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.


0
 
LVL 28

Author Comment

by:iboutchkine
ID: 9922599
Thank you _TAD_ I will try and get back to you.
0
 
LVL 28

Author Comment

by:iboutchkine
ID: 9922872
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now