The System.IO.File class provides several handy methods for working with text files. The following table summarizes these methods.
Method | Purpose |
---|---|
AppendText | Creates a text file or opens it for appending if it already exists. Returns a StreamWriter for writing into the file. |
CreateText | Creates a text file, overwriting it if it already exists. Returns a StreamWriter for writing into the file. |
Exists | Returns True if a file exists. It is better practice (and much faster) to only try to open the file if Exists returns True, rather than just trying to open the file and catching errors with a Try Catch block. |
OpenText | Opens an existing text file and returns a StreamReader to read from it. This method throws a FileNotFoundException if the file doesn’t exist. |