Overview of Types for Files/Directories

See here.

File & Directory IO

All methods return void unless otherwise specified.

Files

GoalUse
Check for a fileFile.Exists (returns bool)
Copy a fileFile.Copy
FileInfo.CopyTo (returns FileInfo)
Delete a fileFile.Delete, FileInfo.Delete
Move/rename a fileFile.Move, FileInfo.MoveTo
Retrieve a full pathPath.GetFullPath
Retrieve file name and extensionPath.GetFileName
Retrieve a file extensionPath.GetExtension
Change a file extensionPath.ChangeExtension

Text Files

GoalUse
Create a text fileFile.CreateText (returns StreamWriter)
File.Create (returns FileStream)
FileInfo.CreateText (returns StreamWriter)
FileInfo.Create (returns FileStream)
Write to a text fileStreamWriter (Write{Async}, WriteLine{Async})
File (WriteAllLines{Async}, WriteAllText{Async})
Append to a text fileFile.AppendText (returns StreamWriter)
FileInfo.AppendText (returns StreamWriter)
Read from a text fileStreamReader

Binary Files

GoalUse
Read fromBinaryReader
Write toBinaryWriter

Directories

GoalUse
Check for a directoryDirectory.Exists (returns bool)
Create a directoryDirectory.CreateDirectory (returns DirectoryInfo)
Create a subdirectoryDirectoryInfo.CreateSubdirectory (returns DirectoryInfo)
Copy a directoryDirectories are copied by creating the directory in the new destination and then copying files to it
Delete a directoryDirectory.Delete, DirectoryInfo.Delete
Move/rename a directoryDirectory.Move, DirectoryInfo.MoveTo

Enumerating Files and Directories [ Documentation]

Enumerable collections provide superior performance vs arrays when working with large collections. The Directory and DirectoryInfo classes have enumeration methods that return enumerable collections:

To retrieveUse
Directory namesDirectory.EnumerateDirectories
DirectoryInfo objectsDirectoryInfo.EnumerateDirectories
File namesDirectory.EnumerateFiles
FileInfo objectsDirectoryInfo.EnumerateFiles
Directory and file namesDirectory.EnumerateFileSystemEntries
FileSystemInfo objectsDirectoryInfo.EnumerateFileSystemInfos

Compression and Decompression

For all examples:

using System.IO.Compression;

Compression and Decompression For zip files

Compression (Creating a zip file):

var startPath = "/source/directory";
var zipFilePath = "/destination/compressed.zip";
zipFile.CreateFromDirectory(startPath, zipFilePath);)

Decompression (Extracting Contents of a zip file):

var zipFilePath = "/destination/compressed.zip";
var destinationPath = "/destination/directory";

// Normalize the destination path:
destinationPath = Path.GetFullPath(destinationPath);

// If the destinationPath does not end with the directory separator character, a path traversal attack is possible:
if (!destinationPath.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal))
    destinationPath += Path.DirectorySeparatorChar;

zipFile.ExtractToDirectory(zipFilePath, destinationPath);

Adding to a zip file

var zipFilePath = "/destination/compressed.zip";

using FileStream zipFilePath = new FileStream(zipFilePath, FileMode.Open);

using ZipArchive archive = new ZipArchive(zipFilePath, ZipArchiveMode.Update);
var fileEntry = archive.CreateEntry("SomeFile.txt");

using StreamWriter writer = new StreamWriter(fileEntry.Open());
writer.WriteLine("Some text...");

Compression and Decompression for GZip

Compression:

var fileToCompress = new FileInfo("/source/file.txt");
using FileStream originalFileStream = fileToCompress.OpenRead();
using FileStream compressedFileStream = File.Create(fileToCompress.FullName + ".gz");
using GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress);
originalFileStream.CopyTo(compressionStream);

Decompression:

var fileToDecompress = new FileInfo("/source/compressed.gz");
using FileStream originalFileStream = fileToDecompress.OpenRead();
using FileStream decompressedFileStream = File.Create("/destination/decompressed.txt");
using GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress);
decompressionStream.CopyTo(decompressedFileStream);