File Class
Summary
Provides static methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of Stream
objects.
Type Definition
public static sealed class File
Members
-
public static void AppendAllLines(string path, IEnumerable<string> contents)Appends lines to a file, and then closes the file. If the specified file does not exist, this method creates a file, writes the specified contents to the file, and then closes the file.Parameters
- path: The file to append to. Each line corresponds to a string in the collection.
- contents: The string collection, each element of which is a line to be written to the file.
-
public static void AppendAllText(string path, string contents)Appends a string to the end of this file. If the file doesn't exist, created, or overwritten, depending on the value of the overwrite parameter.Parameters
- path: The file to append to.
- contents: The string to append.
-
public static void Copy(string sourceFileName, string destinationFileName)Copies an existing file to a new location and optionally overwrites an existing file.Parameters
- sourceFileName: The path to the file to be copied.
- destinationFileName: The path to the new location, which may be a file name.
-
public static void Delete(string path)Deletes the specified file.Parameters
- path: The name of the file to be deleted. Wildcard characters are not permitted.
-
public static bool Exists(string path)Determines whether a file exists.Parameters
- path: The file to check.
Return Valuebool: true if the caller has the required permissions and
path
contains a file; otherwise, false. This method also returns false ifpath
isnull
, an invalid path, or a zero-length string. If the caller does not have sufficient storage to read the file, no exception is thrown and the method returns false. -
public static StreamReader OpenText(string path)Opens a text file for reading with the default encoding.Parameters
- path: The path to the file to be opened.
Return ValueStreamReader: A new StreamReader object for the specified file.
-
public static string[] ReadAllLines(string path)Reads the entire text file into a string array, and then closes the file.Parameters
- path: The file to read.
Return Valuestring[]: A string array containing the lines of the file.
-
public static string ReadAllText(string path)Opens a text file, reads all lines of the file, and then closes the file.Parameters
- path: The file to read.
Return Valuestring: A string containing all lines of the file.
-
public static void WriteAllBytes(string path, byte[] bytes)Creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is overwritten.Parameters
- path: The file to write to.
- bytes: The byte array to write to the file.
-
public static void WriteAllLines(string path, IEnumerable<string> contents)Creates a new file, writes a collection of strings to the file, and then closes the file. If the target file already exists, it is overwritten.Parameters
- path: The file to write to.
- contents: The string collection to write to the file.
-
public static void WriteAllText(string path, string contents)Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.Parameters
- path: The file to write to.
- contents: The string to write to the file.
Example
// Create a file and write some text to it using System.IO; public class Example { public static void Main() { string fileName = "myFile.txt"; string textContent = "This is the content of the file."; // Write all text to the file File.WriteAllText(fileName, textContent); // Read all text from the file string readContent = File.ReadAllText(fileName); Console.WriteLine(readContent); // Append more text to the file File.AppendAllText(fileName, " This is appended text."); // Read all lines from the file string[] lines = File.ReadAllLines(fileName); foreach (string line in lines) { Console.WriteLine(line); } // Delete the file if (File.Exists(fileName)) { File.Delete(fileName); } } }