File copying, byte by byte, w/progress bar
-
Hi. I want to create a dialog which shows the progress of a file copy operation. I'll be copying a batch of files, some of which are up to 40Mb, so I want to display the current file progress aswell as the total progress. The problem I'm having is when I invoke the method for updating the UI. Because I want to update each for file I'm having to cycle through the source file byte by byte and then invoke the method which is using up all my memory. This is what I have so far. Any help would be appreciated. FileCopyDialog.zip[^]
-
Hi. I want to create a dialog which shows the progress of a file copy operation. I'll be copying a batch of files, some of which are up to 40Mb, so I want to display the current file progress aswell as the total progress. The problem I'm having is when I invoke the method for updating the UI. Because I want to update each for file I'm having to cycle through the source file byte by byte and then invoke the method which is using up all my memory. This is what I have so far. Any help would be appreciated. FileCopyDialog.zip[^]
Maby its just me but what are you doing? I see these totaly useless loops, why dont you just use File.ReadAllBytes and then write it out to file again and if you must have a byte by byte view. You can easily loop trough the byte array and write it out one by one. And your why dont you just use the worker report progress function? Well i hope this helps and maby i just dont get the picture.
-
Maby its just me but what are you doing? I see these totaly useless loops, why dont you just use File.ReadAllBytes and then write it out to file again and if you must have a byte by byte view. You can easily loop trough the byte array and write it out one by one. And your why dont you just use the worker report progress function? Well i hope this helps and maby i just dont get the picture.
Cheers for the reply. As I said, I need to display the current transfer progress for each file aswell as the total progress for all files so I need to write byte by byte (which it is doing) and then update the UI's progress bar with the current progress after each byte has been written. I'm not sure which loops you see as unnecessary btw as all 3 of them have a purpose. As for the workers' ProgressChanged event, it only has one value for percent complete and I need two.
modified on Wednesday, July 2, 2008 8:23 AM
-
Hi. I want to create a dialog which shows the progress of a file copy operation. I'll be copying a batch of files, some of which are up to 40Mb, so I want to display the current file progress aswell as the total progress. The problem I'm having is when I invoke the method for updating the UI. Because I want to update each for file I'm having to cycle through the source file byte by byte and then invoke the method which is using up all my memory. This is what I have so far. Any help would be appreciated. FileCopyDialog.zip[^]
First off, ignore the ReadAllBytes suggestion, if your files are 40MB and you do have a few of them then its going to suck up alot of memory. If you copy the file like this, you will be able to keep track of where you are:
const int BUFFERSIZE = 32768; //32KB
int bytesRead = 0;
long totalBytesRead = 0;
long fileSize = 0;
FileStream input = new FileStream("myfile", FileMode.Open);
FileStream output = new FileStream("mydestination", FileMode.Create);
byte[] buffer = new byte[BUFFERSIZE];
fileSize = input.Length;
.
while(totalBytesRead < fileSize)
{
bytesRead = input.Read(buffer, 0, BUFFERSIZE);
output.Write(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
}Don't copy the file one byte at a time either, it will just go really slow. You can see your progress by checking
totalBytesRead
againstfileSize
. Infact, you could set up an event and fire it in the while loop. Your main thread could then use this to update your progress bar etc.My current favourite word is: I'm starting to run out of fav. words!
-SK Genius
-
First off, ignore the ReadAllBytes suggestion, if your files are 40MB and you do have a few of them then its going to suck up alot of memory. If you copy the file like this, you will be able to keep track of where you are:
const int BUFFERSIZE = 32768; //32KB
int bytesRead = 0;
long totalBytesRead = 0;
long fileSize = 0;
FileStream input = new FileStream("myfile", FileMode.Open);
FileStream output = new FileStream("mydestination", FileMode.Create);
byte[] buffer = new byte[BUFFERSIZE];
fileSize = input.Length;
.
while(totalBytesRead < fileSize)
{
bytesRead = input.Read(buffer, 0, BUFFERSIZE);
output.Write(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
}Don't copy the file one byte at a time either, it will just go really slow. You can see your progress by checking
totalBytesRead
againstfileSize
. Infact, you could set up an event and fire it in the while loop. Your main thread could then use this to update your progress bar etc.My current favourite word is: I'm starting to run out of fav. words!
-SK Genius
Legend! Marriage proposal is in the post :)
-
First off, ignore the ReadAllBytes suggestion, if your files are 40MB and you do have a few of them then its going to suck up alot of memory. If you copy the file like this, you will be able to keep track of where you are:
const int BUFFERSIZE = 32768; //32KB
int bytesRead = 0;
long totalBytesRead = 0;
long fileSize = 0;
FileStream input = new FileStream("myfile", FileMode.Open);
FileStream output = new FileStream("mydestination", FileMode.Create);
byte[] buffer = new byte[BUFFERSIZE];
fileSize = input.Length;
.
while(totalBytesRead < fileSize)
{
bytesRead = input.Read(buffer, 0, BUFFERSIZE);
output.Write(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
}Don't copy the file one byte at a time either, it will just go really slow. You can see your progress by checking
totalBytesRead
againstfileSize
. Infact, you could set up an event and fire it in the while loop. Your main thread could then use this to update your progress bar etc.My current favourite word is: I'm starting to run out of fav. words!
-SK Genius
I'm not sure I'd would do this the same way, totalBytesRead would have to be class variable and volatile to display it on the UI. Else the worker has a copy of the original data and can't change it. Why not use a delegate you could invoke to update the UI from the worker?
-Spacix All your skynet questions[^] belong to solved
I dislike the black-and-white voting system on questions/answers. X|
-
I'm not sure I'd would do this the same way, totalBytesRead would have to be class variable and volatile to display it on the UI. Else the worker has a copy of the original data and can't change it. Why not use a delegate you could invoke to update the UI from the worker?
-Spacix All your skynet questions[^] belong to solved
I dislike the black-and-white voting system on questions/answers. X|
-
I'm not sure I'd would do this the same way, totalBytesRead would have to be class variable and volatile to display it on the UI. Else the worker has a copy of the original data and can't change it. Why not use a delegate you could invoke to update the UI from the worker?
-Spacix All your skynet questions[^] belong to solved
I dislike the black-and-white voting system on questions/answers. X|
This is an example of what I've done, just in case anybody else needs something similar in the future. Cheers for your help guys. FileCopyTest.zip[^]