Abstract [ Documentation]

The System.IO namespace contains types to:

  • Read or write files
  • Read or modify directories
  • Read or write data streams
  • Compress or decompress files
  • Communicate via pipes and serial ports

Asynchronous I/O: Synchronous I/O operations block the UI thread until complete. Use asynchronous operations to prevent this.

Overview of Types for Files/Directories

TypeStatic or Instance methodsDescription
FilestaticFor manipulating files, creating FileStream objects
FileInfoinstanceFor manipulating files, creating FileStream objects
DirectorystaticFor manipulating directories
DirectoryInfoinstanceFor manipulating directories
PathbothFor manipulating paths

See Notes on File IO.

Overview of Types for Streams

TypeUse
FileStreamReading/writing a file
IsolatedStorageFileStreamReading/writing a file in isolated storage
MemoryStreamReading/writing to memory as a backing store
BufferedStreamImproving performance of read/write operations
NetworkStreamReading/Writing over network sockets
PipeStreamReading/writing over anonymous and named pipes
CryptoStreamLinking data streams to cryptographic transformations

See Notes on Streams.

Overview of Types for Compression

From the System.IO.Compression namespace:

TypeUse
ZipArchiveCreating and retrieving compressed files in a zip archive
ZipArchiveEntryRepresenting a compressed file
ZipFileCreating, extracting and opening a compressed file
ZipFileExtensionsExtensions for the above
DeflateStreamCompressing and decompressing streams using the Deflate algorithm
GZipStreamCompressing and decompressing streams using the gzip data format

See Notes on Compression and Decompression

Convenience vs. Control

Many of the APIs in the System.IO namespace can be placed on a spectrum of convenience vs control. These APIs are listed from low level (most control) to high level (most convenient)

Reading a Text File

  1. File.OpenHandle + RandomAccess.Read (most control)
  2. File.Open + FileStream.Read`
  3. File.OpenText + StreamReader.ReadLine
  4. File.ReadLines + IEnumerable<string>
  5. File.ReadAllLines + string[]
  6. File.ReadAllText + string (most convenient)

Reading JSON Text

  1. Utf8JsonReader + Pipelines or Stream (most control)
  2. JsonDocument + Stream
  3. JsonSerializer + Stream
  4. JsonSerializer + string (most convenient)