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.WriteAllBytes and File.ReadAllBytes

File.WriteAllBytes and File.ReadAllBytes

Scheduled Pinned Locked Moved C#
question
14 Posts 4 Posters 1 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.
  • I Ian Uy

    Good Day, IF File.WriteAllBytes and File.ReadAllBytes does not have the ability to "return" the progress of the operation, say writing a 1GB file, then what are my other options? My major concern is to be able to extract the progress of the byte writing/reading since I am working with really large files. Thanks! :laugh: ;)

    It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    Can't you read/write them in chunks of arbitrary size, for example 1mb per chunk? regards

    I 1 Reply Last reply
    0
    • L Lost User

      Can't you read/write them in chunks of arbitrary size, for example 1mb per chunk? regards

      I Offline
      I Offline
      Ian Uy
      wrote on last edited by
      #3

      Yes, that is what I'm hoping to achieve. The "older" way to write files. But I need some example codes or a link perhaps. Thank you. ;)

      It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

      L 1 Reply Last reply
      0
      • I Ian Uy

        Good Day, IF File.WriteAllBytes and File.ReadAllBytes does not have the ability to "return" the progress of the operation, say writing a 1GB file, then what are my other options? My major concern is to be able to extract the progress of the byte writing/reading since I am working with really large files. Thanks! :laugh: ;)

        It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

        A Offline
        A Offline
        Adeel Chaudhry
        wrote on last edited by
        #4

        Hello, Perhaps you can use timer and keep an eye on disk space being consumed... Like: 1. Have a timer //do what you want.. 2. Get free space in hard-disk you are playing with big files 3. Play with files in timer tick check free space again, you do know the size of the file right? use these two and check approximately how much work has been done. HTH! Regards, Adeel

        Do rate the reply, if it helps or even if it doesnot, because it helps the members to know, what solved the issue. Thanks.

        I 1 Reply Last reply
        0
        • I Ian Uy

          Yes, that is what I'm hoping to achieve. The "older" way to write files. But I need some example codes or a link perhaps. Thank you. ;)

          It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #5

          You can use BinaryReader.Read[^] in a loop, starting with offset 0 until the end of the file is reached, increasing the offset with the chunk size in every loop step. If you wish I can post an example later (unless you won't find one on google or someone else answers in the meantime), I have to leave right now. regards

          I 1 Reply Last reply
          0
          • A Adeel Chaudhry

            Hello, Perhaps you can use timer and keep an eye on disk space being consumed... Like: 1. Have a timer //do what you want.. 2. Get free space in hard-disk you are playing with big files 3. Play with files in timer tick check free space again, you do know the size of the file right? use these two and check approximately how much work has been done. HTH! Regards, Adeel

            Do rate the reply, if it helps or even if it doesnot, because it helps the members to know, what solved the issue. Thanks.

            I Offline
            I Offline
            Ian Uy
            wrote on last edited by
            #6

            Hi, That approach won't work, unfortunately. I already tried a similar approach using FileSystemWatcher. The problem is that File.WriteAllBytes() will initially write the full file size. I do not know why.

            It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

            A 1 Reply Last reply
            0
            • L Lost User

              You can use BinaryReader.Read[^] in a loop, starting with offset 0 until the end of the file is reached, increasing the offset with the chunk size in every loop step. If you wish I can post an example later (unless you won't find one on google or someone else answers in the meantime), I have to leave right now. regards

              I Offline
              I Offline
              Ian Uy
              wrote on last edited by
              #7

              Please do so if you have time. Thanks! :-D

              It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

              1 Reply Last reply
              0
              • I Ian Uy

                Hi, That approach won't work, unfortunately. I already tried a similar approach using FileSystemWatcher. The problem is that File.WriteAllBytes() will initially write the full file size. I do not know why.

                It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

                A Offline
                A Offline
                Adeel Chaudhry
                wrote on last edited by
                #8

                Why not try out with System.IO.DriveInfo?

                Do rate the reply, if it helps or even if it doesnot, because it helps the members to know, what solved the issue. Thanks.

                I 2 Replies Last reply
                0
                • A Adeel Chaudhry

                  Why not try out with System.IO.DriveInfo?

                  Do rate the reply, if it helps or even if it doesnot, because it helps the members to know, what solved the issue. Thanks.

                  I Offline
                  I Offline
                  Ian Uy
                  wrote on last edited by
                  #9

                  Alright, I'll try it and report back. :)

                  It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

                  1 Reply Last reply
                  0
                  • I Ian Uy

                    Good Day, IF File.WriteAllBytes and File.ReadAllBytes does not have the ability to "return" the progress of the operation, say writing a 1GB file, then what are my other options? My major concern is to be able to extract the progress of the byte writing/reading since I am working with really large files. Thanks! :laugh: ;)

                    It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #10

                    Take a look at the FileStream class. That's the basis of reading and writing files, that's what WriteAllBytes, ReadAllBytes and all the different reader and writer classes use to access files. MSDN Library: FileStream class[^]

                    Despite everything, the person most likely to be fooling you next is yourself.

                    I 1 Reply Last reply
                    0
                    • G Guffa

                      Take a look at the FileStream class. That's the basis of reading and writing files, that's what WriteAllBytes, ReadAllBytes and all the different reader and writer classes use to access files. MSDN Library: FileStream class[^]

                      Despite everything, the person most likely to be fooling you next is yourself.

                      I Offline
                      I Offline
                      Ian Uy
                      wrote on last edited by
                      #11

                      Hi Guffa, My problem is that I don't know how to write a bytearray chunk by chunks. Say I already have byte[] Data, and I want to write it 1024 bytes at a time. Regards

                      It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

                      G 1 Reply Last reply
                      0
                      • I Ian Uy

                        Hi Guffa, My problem is that I don't know how to write a bytearray chunk by chunks. Say I already have byte[] Data, and I want to write it 1024 bytes at a time. Regards

                        It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

                        G Offline
                        G Offline
                        Guffa
                        wrote on last edited by
                        #12

                        Use a loop. Example:

                        int pos = 0;
                        int left = data.Length;
                        while (left > 0) {
                        int len = Math.Min(left, 1024);
                        stream.Write(data, pos, len);
                        pos += len;
                        left -= len;
                        }

                        Despite everything, the person most likely to be fooling you next is yourself.

                        I 1 Reply Last reply
                        0
                        • G Guffa

                          Use a loop. Example:

                          int pos = 0;
                          int left = data.Length;
                          while (left > 0) {
                          int len = Math.Min(left, 1024);
                          stream.Write(data, pos, len);
                          pos += len;
                          left -= len;
                          }

                          Despite everything, the person most likely to be fooling you next is yourself.

                          I Offline
                          I Offline
                          Ian Uy
                          wrote on last edited by
                          #13

                          Exactly what I needed! Thank you! :)

                          It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

                          1 Reply Last reply
                          0
                          • A Adeel Chaudhry

                            Why not try out with System.IO.DriveInfo?

                            Do rate the reply, if it helps or even if it doesnot, because it helps the members to know, what solved the issue. Thanks.

                            I Offline
                            I Offline
                            Ian Uy
                            wrote on last edited by
                            #14

                            Adeel Chaudhry wrote:

                            Why not try out with System.IO.DriveInfo?

                            Hi, I've tried it and the outcome is also the same as using the FileSystemWatcher.

                            It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

                            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