13.2. File Namespace

The File namespace presents you with a number of useful methods that you will want to use in your programs. A partial list of the methods in the File namespace is presented in Table 13-4.

Table 13-4. Table 13-4
MethodDescription
AppendAllText()Appends a string of text to a specified file. The method is overloaded so that different encoding schemes may be used.
AppendText()Uses a StreamWriter object to append UTF-8-encoded text to a specified file. UTF-8 is an eight-bit Unicode Transformation Format that is backward-compatible with the ASCII character set.
Copy()Copies a specified file.
Create()Creates a specified file.
CreateText()Creates or opens a file for UTF-8 encoded text
Delete()Deletes a specified file.
Exists()Checks to see if a specified file exists.
GetCreationTime()Returns the date and time a file was created.
Move()Moves a specified file to a specified location.
Open()Uses a FileStream object to open a specified file.
OpenRead()Opens an existing file for reading.
OpenText()Opens a UTF-8 file for reading.
OpenWrite()Opens an existing file for writing.
ReadAllBytes()Opens a binary file and copies the contents into a byte array. (Each ReadAll*() method has a corresponding WriteAll*() method.)
ReadAllLines()Opens a text file, reads all lines in the file into a string array, and closes it.
ReadAllText()Opens a text file, reads the contents into a string, and closes it.
Replace()Replaces the content of one file with that of another file, deleting the original file and ...

Get Beginning C# 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.