File Copy Progress Bar
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: File Copy Progress Bar

  1. #1
    Join Date
    Oct 2010
    Posts
    3

    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();

    }

  2. #2
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    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.......

  3. #3
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    15,002

    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.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Oct 2010
    Posts
    3

    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

  5. #5
    Join Date
    Oct 2010
    Posts
    3

    Re: File Copy Progress Bar

    And if you can suggest any good really detailed books on learning c sharp it would be most appreciated.

  6. #6
    Join Date
    Aug 2008
    Posts
    902

    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&#37; 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.

  7. #7
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: File Copy Progress Bar

    My hobby projects:
    www.rclsoftware.org.uk

  8. #8
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    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.......

  9. #9
    Join Date
    Jun 2008
    Posts
    2,477

    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

  10. #10
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    15,002

    Re: File Copy Progress Bar

    60mb might not be big, unless it's over a CELLULAR DATA LINE
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  11. #11
    Join Date
    May 2008
    Posts
    36

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center