Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. File copying, byte by byte, w/progress bar

File copying, byte by byte, w/progress bar

Scheduled Pinned Locked Moved C#
helpcsharpdesignperformanceannouncement
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ajtunbridge
    wrote on last edited by
    #1

    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[^]

    S A 2 Replies Last reply
    0
    • A ajtunbridge

      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[^]

      S Offline
      S Offline
      Scalee
      wrote on last edited by
      #2

      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.

      A 1 Reply Last reply
      0
      • S Scalee

        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.

        A Offline
        A Offline
        ajtunbridge
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • A ajtunbridge

          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[^]

          A Offline
          A Offline
          Anthony Mushrow
          wrote on last edited by
          #4

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

          Game Programming articles start -here[^]-

          A S 2 Replies Last reply
          0
          • A Anthony Mushrow

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

            Game Programming articles start -here[^]-

            A Offline
            A Offline
            ajtunbridge
            wrote on last edited by
            #5

            Legend! Marriage proposal is in the post :)

            1 Reply Last reply
            0
            • A Anthony Mushrow

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

              Game Programming articles start -here[^]-

              S Offline
              S Offline
              Spacix One
              wrote on last edited by
              #6

              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|


              A A 2 Replies Last reply
              0
              • S Spacix One

                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|


                A Offline
                A Offline
                Anthony Mushrow
                wrote on last edited by
                #7

                I did say you could set up an event and trigger that during the loop, you would pass a copy of totalBytesRead and fileSize through that to the main thread.

                My current favourite word is: I'm starting to run out of fav. words!

                -SK Genius

                Game Programming articles start -here[^]-

                1 Reply Last reply
                0
                • S Spacix One

                  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|


                  A Offline
                  A Offline
                  ajtunbridge
                  wrote on last edited by
                  #8

                  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[^]

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups