Stream Handling
Another way to interact with disk files is via streamsclasses that allow for flexibility in the way you use your underlying disk files. For example, with streams, you can specify sharing mode if you want multiple processes to access the same file. Also, you can restrict the access rights on a given file (read-only, write-only, or read-write). In other words, streams provide a powerful pattern-driven mechanism for disk access. Let's look at some code in Listing 3 to help make this principle more concrete.
Listing 3Using stream objects.
FileInfo aFile = null; FileStream fileStream = null; aFile = new FileInfo(@"C:\MyNewFile.txt"); fileStream = aFile.Open(FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None);
In Listing 3, I create a new file object and then open a stream against that file. Once the stream is opened, I can start to read, write, seek, and so on. Before we start to do any of these operations, let's make a few more additions to the exception-handling code.