These buffers are generally put in place for

Info icon This preview shows pages 448–450. Sign up to view the full content.

View Full Document Right Arrow Icon
These buffers are generally put in place for performance reasons. Although the default buffer sizes are chosen for a reasonable trade-off between performance and robustness, for an I/O-intensive application, you may need to hand-tune this using the appropriate constructors on FileStream . As usual, you can do more harm than good if you don’t measure the impact on performance carefully on a suitable range of your target sys- tems. Most applications will not need to touch this value. Even if you don’t need to tune performance, you still need to be aware of buffering for robustness reasons. If either the process or the OS crashes before the buffers are written out to the physical disk, you run the risk of data loss (hence the reason write buffering is typically disabled on the server). If you’re writing frequently to a Stream or StreamWriter , the .NET Framework will flush the write buffers periodically. It also ensures that everything is properly flushed when the stream is closed. However, if you just stop writing data but you leave the stream open, there’s a good chance data will hang around in memory for a long time without getting written out, at which point data loss starts to become more likely. In general, you should close files as early as possible, but sometimes you’ll want to keep a file open for a long time, yet still ensure that particular pieces of data get written out. If you need to control that yourself, you can call Flush . This is particularly useful if you have multiple threads of execution accessing the same stream. You can synchronize writes and ensure that they are flushed to disk before the next worker gets in and messes things up! Later in this chapter, we’ll see an example where explicit flushing is extremely important. Setting Permissions During Construction Another parameter we can set in the constructor is the FileSystemRights . We used this type earlier in the chapter to set filesystem permissions. FileStream lets us set these directly when we create a file using the appropriate constructor. Similarly, we can also specify an instance of a FileSecurity object to further control the permissions on the underlying file. 424 | Chapter 11: Files and Streams
Image of page 448

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Setting Advanced Options Finally, we can optionally pass another enumeration to the FileStream constructor, FileOptions , which contains some advanced filesystem options. They are enumerated in Table 11-9 . This is a flags-style enumeration, so you can combine these values. Table 11-9. FileOptions enumeration FileOptions Purpose None No options at all. WriteThrough Ignores any filesystem-level buffers, and writes directly to the output device. This affects only the O/S, and not any of the other layers of buffering, so it’s still your responsibility to call Flush .
Image of page 449
Image of page 450
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern