CHAPTER 20 FILE I/O AND OBJECT SERIALIZATION
WriteAllBytes()
Opens the specified file, writes out the byte array, and then closes the file.
WriteAllLines()
Opens a specified file, writes out an array of strings, and then closes the
file.
WriteAllText()
Opens a specified file, writes the character data from a specified string,
and then closes the file.
You can use these methods of the File type to read and write batches of data in only a few lines of
code. Even better, each of these members automatically closes down the underlying file handle. For
example, the following console program (named SimpleFileIO) persists the string data into a new file on
the C: drive (and reads it into memory) with minimal fuss (this example assumes you have imported
System.IO):
class Program
{
static void Main(string[] args)
{
Console.WriteLine("***** Simple I/O with the File Type *****\n");
string[] myTasks = {
"Fix bathroom sink", "Call Dave",
"Call Mom and Dad", "Play Xbox 360"};
// Write out all data to file on C drive.
File.WriteAllLines(@"C:\tasks.txt", myTasks);