Best web design - // Get some StreamWriters. StreamWriter swriter = File.CreateText(@”C:Test3.txt”);
// Get some StreamWriters. StreamWriter swriter = File.CreateText(@”C:Test3.txt”); swriter.Close(); StreamWriter swriterAppend = File.AppendText(@”C:FinalTest.txt”); swriterAppend.Close(); } New .NET 2.0 File Members Unlike FileInfo, the File type supports a few unique members (as of .NET 2.0) shown in Table 16-5, which can greatly simplify the processes of reading and writing textual data. Table 16-5. 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 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, and then closes the file Using these new methods of the File type, you are able to read and write batches of data in just a few lines of code. Even better, each of these new members automatically closes down the underlying file handle, for example: class Program { static void Main(string[] args) { string[] myTasks = { “Fix bathroom sink”, “Call Dave”, “Call Mom and Dad”, “Play XBox”}; // Write out all data to file on C drive. File.WriteAllLines(@”C:tasks.txt”, myTasks); // Read it all back and print out. foreach (string task in File.ReadAllLines(@”C:tasks.txt”)) { Console.WriteLine(”TODO: {0}”, task); } } } Clearly, when you wish to quickly obtain a file handle, the File type will save you some keystrokes. However, one benefit of first creating a FileInfo object is that you are able to investigate the file using the members of the abstract FileSystemInfo base class: CHAPTER 16 530 THE SYSTEM.IO NAMESPACE
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.