Read and write into file without opening the stream

Its quite often you need to write the code to read or write or append some simple text into a text file. In-general you open a file stream and read the text and finally you will close the file stream. There is an easiest way to do this. The System.IO.File static class provides a method to read/write/append text without opening and closing the streams explicitly. The method automatically opens the stream and closes the stream after you read it.


string content = System.IO.File.ReadAllText("filepath")
System.IO.File.WriteAllText("filepath", "content")
System.IO.File.AppendAllText("filepath", "content")

No comments:

Post a Comment