problem | |
c# - how to handle files, directories and drives --- Working with the File Type --- in practice! >>guide<< | |
difficulty level | |
3/10 :| | |
compatibility | |
flex 4.0 | |
solution | |
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, the following console program (named SimpleFileIO) will persist the string data into a new file on the C drive (and read it into memory) with minimal fuss: using System; using System.IO; // important! class Program { static void Main(string[] args) { Console.WriteLine("***** Simple IO 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); // Read it all back and print out. foreach (string task in File.ReadAllLines(@"C:\tasks.txt")) Console.WriteLine("TODO: {0}", task); Console.ReadLine(); } } |
Developing with CSharp many time you feel a strange feeling... then feeling of lost and how to solve it. Fortunately MSDN has done a good work to help the developers. .net offers thousand ways to do several things! This is the worst thing… because, you do not know where to start from and what works in real world! I hope to help you with your CSharpache.
Thursday, 23 June 2011
how to handle files, directories and drives
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment