0

I always use File.WriteAllBytes, but it's not working if the file is larger than my RAM available. Is it possible to write the file byte per byte and show the progress in a progress bar? If possible, can I do it in FileStream?

1
  • Where are the bytes coming from? How many do you receive at once? Commented Jun 1, 2014 at 1:41

4 Answers 4

1

you can definitely do that.

I hope that following information will help you out.

http://msdn.microsoft.com/en-us/library/system.io.filestream.write(v=vs.110).aspx

if you provide some code that it is great to help you out.

Thanks.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use a BinaryWriter to write either byte by byte or, better, byte[] by byte[] (probably more efficient : truncate your bytes stream in reasonably sized chunks)

    using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
    {
        bytes[] nextBytes = GetNextBytes() // your logic to get what to write;
        writer.Write(nextBytes);
    }

As for the progress bar, this is a different question, but you could use a BackgroundWorker to report (how_many_bytes_you_wrote/total_bytes_to_write ) to a ProgressBar on your GUI.

2 Comments

Yes I do use background worker for both progress reporting and to prevent freezing. So I can simply call BinaryWriter.Write and place next bytes and the file size will be added, not replaced?
FileMode.Create (in my post) overwrite the existing file. You can replace by FileMode.Append to append the data to an existing file
0

Don't do that. Writing one byte at a time is not possible at the lower layers of I/O. If buffering is disabled, you'll end up with the following sequence of events:

  • Read a disk sector, if it isn't already in the file cache
  • Update one byte
  • Write the entire sector back to disk

When buffering is enabled, you're still making a bunch of extra kernel calls to update the file cache. Kernel calls are one of the more expensive things your program does. One is nowhere near as expensive as actual disk I/O, but 16000 are.

Instead, pick a block size that fits in memory, is a multiple of the disk cluster size and virtual member page size, and does a useful amount of work. Less than 64kB is just too much overhead for individual requests. More than 4 MB has pretty much no benefit. Also consider how many times to need to update the progress bar in order to make updates "smooth" (having more chunks than pixels in the progress bar width is not visible, and just wasted UI update processing in addition to the I/O requests).

Then, use the FileStream.Write overload that takes a byte array and writes all or part of it.

Comments

-1

Try use this BinaryWriter.Write visit: http://msdn.microsoft.com/en-us/library/6tky7ax3%28v=vs.110%29.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.