-
October 19th, 2010, 06:29 AM
#1
File Copy Progress Bar
I have a 60mb file im trying to copy while showing progress in a progressbar1
Current code below takes way to long and errors at the end of the file
all im looking for is a simple
File.Copy(openFileDialog1.FileName, System.Windows.Forms.Application.StartupPath + @"\rawdump.bin", true);
With progressbar1 filling up till end of copy why i something so simple this complex:
progressBar1.Value = progressBar1.Minimum;
System.IO.BinaryReader Reader = new System.IO.BinaryReader(new System.IO.FileStream(openFileDialog1.FileName, System.IO.FileMode.Open), Encoding.ASCII);
System.IO.BinaryWriter Writer = new System.IO.BinaryWriter(new System.IO.FileStream(System.Windows.Forms.Application.StartupPath + @"\rawdump.bin", System.IO.FileMode.Create));
progressBar1.Maximum = (int)Reader.BaseStream.Length;
while (Reader.PeekChar() != -1)
{
Writer.Write(Reader.ReadByte());
progressBar1.PerformStep();
Writer.Flush();
}
int nBytes = (int)Reader.BaseStream.Length;
progressBar1.Maximum = nBytes;
for (int i = 0; i < nBytes; i++)
{
Writer.Write(Reader.ReadByte());
progressBar1.PerformStep();
}
-
October 19th, 2010, 07:40 PM
#2
Re: File Copy Progress Bar
Do the copy in a BackgroundWorker class (RunWorkerAsync) and use the ReportProgess event to notify your UI's Progress bar of the percentage complete....
Drag a BackgroundWorker control onto your form, you will see how it works...
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
October 19th, 2010, 08:00 PM
#3
Re: File Copy Progress Bar
You are copying 1 BYTE at a time? Why?
My HARD DRIVE has a built-in 32MB BUFFER built-in. I can write a file in a few seconds, then shut down the machine. The drive doesn't stop until the buffer is empty.
Hint: Read BLOCKS of data. Set up a BLKSIZE, and keep track of it's location.
-
October 19th, 2010, 09:59 PM
#4
Re: File Copy Progress Bar
Well how can i go about doing more the one byte at a time the byte builds the progressbar
this there another wait to build the bar in chucks
-
October 19th, 2010, 10:12 PM
#5
Re: File Copy Progress Bar
And if you can suggest any good really detailed books on learning c sharp it would be most appreciated.
-
October 20th, 2010, 03:54 AM
#6
Re: File Copy Progress Bar
Uh, well you are setting the maximum value of the progress bar to the size of the file in bytes and the value of the bar to the number of bytes written.
I guess that's one way to do it.
Why don't you instead simply have the bars values go from 0 to 100 as in 0-100% which makes more sense.
Bytes Written / Total File Size * 100 = The completion percentage
and it doesn't matter how many bytes you transfer per go obviously.
-
October 20th, 2010, 05:32 AM
#7
Re: File Copy Progress Bar
-
October 21st, 2010, 04:40 AM
#8
Re: File Copy Progress Bar
There are many options, but I think BackgroundWorker is the one for you. However, you should copy the whole file at once, not a byte at a time. Use ReportProgress if you are copying say 10 files. Assuming equal size, increment the progress bar by 10% each time one completes.
You say this should be a simple task... But if you look closely, even Microsoft and Apple can't get it right... How many times have you sat stairing at a ProgressBar for over a minute, waiting for the ONE second remaining to complete? It's not easy at all.
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
October 21st, 2010, 02:15 PM
#9
Re: File Copy Progress Bar
rlinq, I would give you rep for that if I could.
@OP: Yes, please don't slow down the process of copying a file just to show a progress bar. You will not create the most accurate progress bar ever made, and that's ok. Really, it's ok, move on with your project and your life
-
October 21st, 2010, 07:03 PM
#10
Re: File Copy Progress Bar
60mb might not be big, unless it's over a CELLULAR DATA LINE
-
October 25th, 2010, 05:57 AM
#11
Re: File Copy Progress Bar
Copy the file in chunks on one thread and use callbacks to set the progress bar value.
On my own laptop through trial and error I found that the lowest amount I could read but still be io efficient was about half a meg.
Code:
//Code was not complied\checked, more of guide, notice I havnt even closed the streams
void Copy()
{
int halfAMeg = (int)(1024 * 1024 * 0.5);
FileStream strIn = new FileStream("filePathIn", FileMode.Open);
FileStream strOut = new FileStream("filePathOut", FileMode.Create);
byte[] buf = new byte[halfAMeg];
while (strIn.Position < strIn.Length)
{
int len = strIn.Read(buf, 0, buf.Length);
strOut.Write(buf, 0, len);
SetProBar(strIn.Position, strIn.Length);
}
}
private delegate void SetProBar_CallBack(long val,long max);
private void SetProBar(long val, long max)
{
if (progressBar.InvokeRequired)
{
SetProBar_CallBack callBack = new SetProBar_CallBack(SetProBar);
this.Invoke(callBack, new object[] { val, max });
}
else
{
progressBar.Maximum = Int32.MaxValue;
progressBar.Value = (int)(Int32.MaxValue / (max / val));
}
}
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
|