15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

eading <strong>and</strong> Writing to files ❘ 791<br />

In addition to these methods, FileStream implements various other methods <strong>and</strong> properties related to<br />

bookkeeping tasks such as determining how many bytes are in the stream, locking the stream, or flushing<br />

the buffer. These other methods are not usually required for basic reading <strong>and</strong> writing, but if you need<br />

them, full details are in the SDK documentation.<br />

Binaryfilereader sample<br />

The use of the FileStream class is illustrated by writing a sample, BinaryFileReader, which reads in <strong>and</strong><br />

displays any file. Create the project in Visual Studio 2010 as a Windows application. It has one menu item,<br />

which brings up a st<strong>and</strong>ard OpenFileDialog asking what file to read in <strong>and</strong> then displays the file as binary<br />

code. As you are reading in binary files, you need to be able to display nonprintable characters. You will do<br />

this by displaying each byte of the file individually, showing 16 bytes on each line of a multiline text box.<br />

If the byte represents a printable ASCII character, you will display that character; otherwise, you will<br />

display the value of the byte in a hexadecimal format. In either case, you pad out the displayed text with<br />

spaces so that each byte displayed occupies four columns; this way, the bytes line up nicely under each other.<br />

Figure 29-10 shows what the BinaryFileReader application looks like when viewing a text file. (Because<br />

BinaryFileReader can view any file, it is quite possible to use it on text files as well as binary ones.) In this<br />

case, the application has read in a basic ASP.<strong>NET</strong> page (.aspx).<br />

figure 29-10<br />

Clearly, this format is more suited for looking at the values of individual bytes than for displaying text!<br />

Later in this chapter, when you develop a sample that is specifically designed to read text files, you will see<br />

what this file really says. The advantage of this sample is that you can look at the contents of any file.<br />

This sample does not demonstrate writing to files because you don’t want to get bogged down in the<br />

complexities of trying to translate the contents of a text box as the one shown in Figure 29-10 into a binary<br />

stream! You see how to write to files later when you develop a sample that can read or write, but only to <strong>and</strong><br />

from text files.<br />

Here is the code used to get these results. First, you need to make sure that you have brought in the<br />

System.IO namespace through the use of the using statement:<br />

using System.IO;<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!