File APIs
Streams are important and very useful to understand, but sometimes you just wanna read a text file into memory, do some stuff, and then move on with your life. The File
API provides common file methods for .NET developers.
File
Class
The File
class provides static methods for working with files.
File.Exists
- Checks if a file exists at the specified path.
File.ReadAllText
vs File.ReadAllLines
vs File.ReadAllBytes
- ReadAllText: Reads the contents of a file as a single string.
- ReadAllLines: Reads the contents of a file and returns an array of strings, each representing a line in the file.
- ReadAllBytes: Reads the contents of a file as a byte array.
string content = File.ReadAllText("file.txt");
string[] lines = File.ReadAllLines("file.txt");
byte[] bytes = File.ReadAllBytes("file.txt");
File.WriteAllText
vs File.AppendAllText
vs File.WriteAllLines
- WriteAllText: Writes text to a file, overwriting the file if it already exists.
- AppendAllText: Appends text to a file, creating the file if it doesn't exist.
- WriteAllLines: Writes an array of strings to a file, each string being a line in the file.
File.WriteAllText("file.txt", "Hello, World!");
File.AppendAllText("file.txt", "\nGoodbye, World!");
File.WriteAllLines("file.txt", new[] { "Hello", "World" });
File.Copy
- Copies an existing file to a new file, optionally overwriting the destination file.
File.Copy("source.txt", "destination.txt");
File.Move
- Moves a file to a new location, optionally overwriting the destination file.
File.Move("source.txt", "destination.txt");
File.Delete
- Deletes a specified file.
File.Delete("file.txt");
FileInfo
Class
The FileInfo
class provides instance methods and properties for working with files.
new FileInfo
Creates a new FileInfo
instance for a specified file path.
var fileInfo = new FileInfo("file.txt");
Common Properties
- Length: Gets the size of the file in bytes.
- CreationTime: Gets or sets the creation date and time of the file.
- LastAccessTime: Gets or sets the date and time the file was last accessed.
var fileInfo = new FileInfo("file.txt");
Console.WriteLine(fileInfo.Length);
Console.WriteLine(fileInfo.CreationTime);
Console.WriteLine(fileInfo.LastAccessTime);
Common Methods
Delete: Deletes the file represented by the FileInfo
instance.
fileInfo.Delete();
DirectoryInfo and Directory
The DirectoryInfo
and Directory
classes provide methods for working with directories.
Directory.CreateDirectory
Creates a directory and any subdirectories specified in the path. If the directory already exists, no action is taken, so it's safe to call this method multiple times.
Directory.CreateDirectory("newDirectory");
Path
Class
The Path
class provides static methods for manipulating path strings.
Path.Combine
Combines two or more strings into a path.
string path = Path.Combine("directory", "file.txt");
//output: "directory\file.txt"
Path.GetDirectoryName
Gets the directory information for the specified path string.
string directory = Path.GetDirectoryName("file.txt");
Path.GetExtension
Gets the extension of the specified path string.
string extension = Path.GetExtension("file.txt");
Path.GetRandomFileName
Gets a random file name.
string randomFileName = Path.GetRandomFileName();
Path.GetTempFileName
Gets the full path to a temporary file. This file is actually created inside of the user's temp directory. Useful for creating a temporary file and then deleting it after the program is done using it.
string tempFileName = Path.GetTempFileName();