|
-
Sep 24th, 2008, 07:53 PM
#1
Thread Starter
New Member
[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.
-
Sep 24th, 2008, 08:08 PM
#2
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.
-
Sep 24th, 2008, 11:39 PM
#3
Thread Starter
New Member
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.
-
Sep 24th, 2008, 11:54 PM
#4
Re: [2008] Custom File Copy Code
E.g.
vb.net Code:
Private Sub CopyFile(ByVal sourcePath As String, _ ByVal destinationPath As String, _ ByVal blockSize As Integer) Using source As New IO.FileStream(sourcePath, IO.FileMode.Open) Using destination As New IO.FileStream(destinationPath, IO.FileMode.Create) Dim buffer(blockSize - 1) As Byte Dim byteCount As Integer = source.Read(buffer, 0, blockSize) While byteCount > 0 destination.Write(buffer, 0, byteCount) byteCount = source.Read(buffer, 0, blockSize) End While End Using End Using End Sub
-
Oct 3rd, 2008, 09:48 PM
#5
Thread Starter
New Member
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.
-
Oct 4th, 2008, 08:39 AM
#6
Re: [2008] Custom File Copy Code
Exactly what exceptions would these be?
-
Oct 6th, 2008, 02:01 AM
#7
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|