Free mag vol1 | Page 821

CHAPTER 20  FILE I/O AND OBJECT SERIALIZATION { // Obtain FileStream object via File.Create(). using(FileStream fs = File.Create(@"C:\Test.dat")) {} // Obtain FileStream object via File.Open(). using(FileStream fs2 = File.Open(@"C:\Test2.dat", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)) {} // Get a FileStream object with read-only permissions. using(FileStream readOnlyStream = File.OpenRead(@"Test3.dat")) {} // Get a FileStream object with write-only permissions. using(FileStream writeOnlyStream = File.OpenWrite(@"Test4.dat")) {} // Get a StreamReader object. using(StreamReader sreader = File.OpenText(@"C:\boot.ini")) {} // Get some StreamWriters. using(StreamWriter swriter = File.CreateText(@"C:\Test6.txt")) {} using(StreamWriter swriterAppend = File.AppendText(@"C:\FinalTest.txt")) {} } Additional File-Centric Members The File type also supports a few members, shown in Table 20-6, which can greatly simplify the processes of reading and writing textual data. Table 20-6. Methods of the File Type Method Meaning in Life ReadAllBytes() Opens the specified file, returns the binary data as an array of bytes, and then closes the file. ReadAllLines() Opens a specified file, returns the character data as an array of strings, and then closes the file. ReadAllText() Opens a specified file, returns the character data as a System.String, and then closes the file. 767