Results 1 to 7 of 7

Thread: [RESOLVED] [2008] Custom File Copy Code

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    9

    Resolved [RESOLVED] [2008] Custom File Copy Code

    I'm working on a program which deals with transferring files to a user-specified directory which in most cases will be on another computer. It currently works just fine using My.Computer.CopyFile and My.Computer.FileSystem.CopyDirectory as well as a progress bar.

    The problem is I would like to program in an accurate progress bar and potentially even calculate the estimate time remaining. I currently have it updating based on file size as each file or directory is finished, but that works terribly when large files or directories are being used.

    After extensive Googling, searching MSDN, and searching the forums, I'm still having issues figuring out what to do. I figured out how to give the standard Windows file copy dialog, but I need to updating things on my form and assessing transfer time in my own code. The best I could find online is that I most likely need to use BinaryReader & BinaryWriter or IO.FileStream and create my own code for moving files. Unfortunately, the few examples of even remotely relevant code I can find online are either for previous version of VB (and don't work) or for C++ or C#.

    Can anyone point me in the right direction here? A good VB.NET 2008 code example or article with instructions would be enough here.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Custom File Copy Code

    If you want to be able to give the user accurate progress of your own calculation you're going to have to copy the files byte by byte yourself. That would mean creating a FileStream on the source file and reading it byte by byte, then writing that data byte by byte to a FileStream created on the source file. I strongly recommend against doing this. You're most likely going to slow the process down by doing so, for one thing.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    9

    Re: [2008] Custom File Copy Code

    This is the conclusion I've already come to -- I just have no idea how to do it or where to even start. I'm aware that it will slow down the process, but I'm willing to risk that. Even if it's difficult, I'm up to the challenge; I just don't know where to start.

    Edit: I should specify: I've been trying to figure out how to do a filestream but it's not quite a self-explanatory as other VB functions and half the Google hits end up being about Streamreaders, which isn't helpful. I'm not finding anything good on doing a filestream to copy binary files.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Custom File Copy Code

    E.g.
    vb.net Code:
    1. Private Sub CopyFile(ByVal sourcePath As String, _
    2.                      ByVal destinationPath As String, _
    3.                      ByVal blockSize As Integer)
    4.     Using source As New IO.FileStream(sourcePath, IO.FileMode.Open)
    5.         Using destination As New IO.FileStream(destinationPath, IO.FileMode.Create)
    6.             Dim buffer(blockSize - 1) As Byte
    7.             Dim byteCount As Integer = source.Read(buffer, 0, blockSize)
    8.  
    9.             While byteCount > 0
    10.                 destination.Write(buffer, 0, byteCount)
    11.                 byteCount = source.Read(buffer, 0, blockSize)
    12.             End While
    13.         End Using
    14.     End Using
    15. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    9

    Re: [2008] Custom File Copy Code

    Thanks! This has been working beautifully. I finally saw your post a couple nights a go and I just finished implenting it. I managed to get it working the way I want it, with one major caveat.

    It seems to throw up more and more exceptions the larger the block size is. The issue is that the larger I set the block size, the faster it goes (I know, I've been warned that this will be slow). It's almost comical. Setting the block size to a KiB results in transfer rates of just a few KiB a second. I found 10 MiBs was pretty reasonable, but that throws up exceptions on smaller files. I ended up having block size be based on how much data I'm copying. This is because gives me lots of control over my progress bar while still maintaining a reasonable speed despite the size of the data. This throws up exeptions on pretty much everything, however.

    My understanding of block size and how these exceptions are is rather lacking. Does the block size have to evenly divide into the file so that an exception isn't thrown when it gets to the last block (of my block size)? If not, what do I do to avoid these exceptions? Or what am I missing?

    Thanks again.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Custom File Copy Code

    Exactly what exceptions would these be?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    9

    Re: [2008] Custom File Copy Code

    The ones I made when I wasn't calculating my progress bar properly and was doing it with the same try... catch as my file copy code. There are no exceptions anymore, and the code works wonderfully. By setting the block size to 1% of the total amount of data to be copy, I get a good balance between speed and frequent updates. I'm on to other problems now. Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width