File Class
Represents a static class that provides methods for creating, copying, deleting, moving, and opening files. This class cannot be inherited.
Summary
The System.IO.File
class is a fundamental part of .NET's file system operations. It provides a set of static methods that allow developers to interact with files on the local file system without needing to create an instance of a file object. This makes common file operations concise and efficient.
Members
Methods
AppendAllLines
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 lines to the file, and then closes the file.
public static void AppendAllLines(string path, IEnumerable<string> contents);
public static void AppendAllLines(string path, IEnumerable<string> contents, System.Text.Encoding encoding);
AppendAllText
Opens a text file, appends the specified string to the file, and then closes the file. If the file already exists, then its contents are appended. If the file does not exist, this method creates a file to hold the specified contents.
public static void AppendAllText(string path, string contents);
public static void AppendAllText(string path, string contents, System.Text.Encoding encoding);
Copy
Copies an existing file to a new location.
public static void Copy(string sourceFileName, string destFileName);
public static void Copy(string sourceFileName, string destFileName, bool overwrite);
Create
Creates or overwrites a file in the specified path and returns a new FileStream object that provides access to the file.
public static System.IO.FileStream Create(string path);
public static System.IO.FileStream Create(string path, int bufferSize);
Move
Moves an existing file to a new location.
public static void Move(string sourceFileName, string destFileName);
ReadAllBytes
Opens a file, reads all bytes from the file, and then closes the file.
public static byte[] ReadAllBytes(string path);
ReadAllLines
Opens a text file, reads all lines of the file, and then closes the file.
public static string[] ReadAllLines(string path);
public static string[] ReadAllLines(string path, System.Text.Encoding encoding);
ReadAllText
Opens a file, reads all lines of the file, and then closes the file.
public static string ReadAllText(string path);
public static string ReadAllText(string path, System.Text.Encoding encoding);
WriteAllBytes
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.
public static void WriteAllBytes(string path, byte[] bytes);
WriteAllLines
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.
public static void WriteAllLines(string path, IEnumerable<string> contents);
public static void WriteAllLines(string path, IEnumerable<string> contents, System.Text.Encoding encoding);
WriteAllText
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.
public static void WriteAllText(string path, string contents);
public static void WriteAllText(string path, string contents, System.Text.Encoding encoding);
Properties
The File
class is static and does not have instance properties.
Events
The File
class is static and does not raise events.
Remarks
All file path parameters for the File
class are strings. These strings can represent relative or absolute paths. Relative paths are interpreted relative to the current working directory.
When performing operations that read from or write to files, ensure that the application has the necessary file system permissions. Otherwise, a System.UnauthorizedAccessException
will be thrown.