15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

798 ❘ ChaPTer 29 mAnipulAtinG files And the reGistry<br />

that you will be interested in: Add(), which adds a string to the collection, <strong>and</strong> CopyTo(), which copies the<br />

string collection into a normal array (a System.Array instance). Each element of the StringCollection<br />

object holds one line of the file.<br />

The DisplayFile() method calls another method, ReadFileIntoStringCollection(), which actually<br />

reads in the file. After doing this, you now know how many lines there are, so you are in a position to<br />

copy the StringCollection into a normal, fixed-size array <strong>and</strong> feed it into the text box. Because only the<br />

references to the strings, not the strings themselves, are copied when you actually make the copy, the process<br />

is reasonably efficient:<br />

void DisplayFile()<br />

{<br />

StringCollection linesCollection = ReadFileIntoStringCollection();<br />

string [] linesArray = new string[linesCollection.Count];<br />

linesCollection.CopyTo(linesArray, 0);<br />

this.textBoxContents.Lines = linesArray;<br />

}<br />

code download ReadWriteText.sln<br />

The second parameter of StringCollection.CopyTo() indicates the index within the destination array of<br />

where you want the collection to start.<br />

Now you examine the ReadFileIntoStringCollection() method. You use a StreamReader to read in<br />

each line. The main complication here is the need to count the characters read in to make sure that you do<br />

not exceed the capacity of the text box:<br />

StringCollection ReadFileIntoStringCollection()<br />

{<br />

const int MaxBytes = 65536;<br />

StreamReader sr = new StreamReader(chosenFile);<br />

StringCollection result = new StringCollection();<br />

int nBytesRead = 0;<br />

string nextLine;<br />

while ( (nextLine = sr.ReadLine()) != null)<br />

{<br />

nBytesRead += nextLine.Length;<br />

if (nBytesRead > MaxBytes)<br />

break;<br />

result.Add(nextLine);<br />

}<br />

sr.Close();<br />

return result;<br />

}<br />

That completes the code for this sample.<br />

code download ReadWriteText.sln<br />

If you run ReadWriteText, read in the Default.aspx file, <strong>and</strong> then save it, the file will be in Unicode<br />

format. You would not be able to tell this from any of the usual Windows applications. Notepad, WordPad,<br />

<strong>and</strong> even the ReadWriteText example will still read the file in <strong>and</strong> display it correctly under most versions<br />

of Windows, although, because Windows 9x doesn’t support Unicode, applications like Notepad won’t be<br />

able to underst<strong>and</strong> the Unicode file on those platforms. (If you download the example from the Wrox Press<br />

web site at www.wrox.com, you can try this!) However, if you try to display the file again using the earlier<br />

BinaryFileReader sample, you can see the difference immediately, as shown in Figure 29-12. The two<br />

initial bytes that indicate the file is in Unicode format are visible, <strong>and</strong> thereafter you see that every character<br />

is represented by 2 bytes. This last fact is obvious because the high-order byte of every character in this<br />

particular file is zero, so every second byte in this file now displays x00.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!