1

I've found out that there is no point in using BufferedStream in conjunction with FileStream as it has it's own buffering strategy. Yet, I was wondering about one thing:

FileStream fsWithBuffer = new FileStream("buf.dat", FileMode.OpenOrCreate,
                FileAccess.ReadWrite, FileShare.None, 255);
            fsWithBuffer.WriteByte((byte)4);
            fsWithBuffer.Dispose();

This codes write one portion of byte into the specified file. Before that, this byte is kept in inner buffer, so I understand that if I didn't call Dispose() method, nothing would be written to the file.

Now, my question is: sometimes we don't want to put all of the data in a buffer of a FileStream, let's say it is meant to be only for small writes. Is there a possibility to put some data directly into the file associated with FileStream (without putting it earlier into inner buffer)?

Thanks!

Paweł Poręba
  • 1,084
  • 1
  • 14
  • 37
  • 1
    Are you asking if you can use `FileStream` without a buffer? I don't see the point, you still have to dispose of the FileStream class. – Lasse V. Karlsen Aug 28 '15 at 08:33
  • 1
    You could always call `Flush`, although you won't get around the buffer this way (and I dont't see the point of wanting to do this without one). – KeyNone Aug 28 '15 at 08:33
  • 1
    Okay, I see. Thank You:) Anyway, I guess Flush is what I was looking for in this case. – Paweł Poręba Aug 28 '15 at 08:38

2 Answers2

3

There is a Flag Enum FileOptions.WriteThrough value that can be passed to the FileStream constructor. It instructs the file stream to directly write to the underlying data storage without any buffering.

CodeTherapist
  • 2,776
  • 14
  • 24
1

To sum up this question, as there was no official answer, here what I was looking for was the Flush() method. It saves the actual buffer to the file associated with FileStream and it was something I needed :)

Paweł Poręba
  • 1,084
  • 1
  • 14
  • 37