System.IO Namespace
Provides types that allow reading and writing files and data streams, and types that represent and manage different kinds of I/O operations.
Key Types
File
Provides static methods for the creation, copying, deletion, moving, and opening of files.
Directory
Provides static methods for the creation, moving, and enumeration of directories and subdirectories.
Path
Contains a set of static properties and instance methods that are useful for creating, manipulating, and converting directory and file path strings.
Stream
Abstract base class and a representative of a stream of bytes. Provides a base for all streams.
FileInfo
Provides properties and instance methods for the creation, deletion, moving, and opening of files.
DirectoryInfo
Provides properties and instance methods for the creation, deletion, moving, and enumeration of directories and subdirectories.
StreamWriter
Implements a TextWriter
for writing characters to a stream in a particular encoding.
MemoryStream
Creates a stream that supports both reading and writing by using an array of bytes as the backing store.
FileStream
Exposes a Stream
around a file, supporting both synchronous and asynchronous read and write operations.
Common Operations
The System.IO
namespace is fundamental for any application that interacts with the file system or network streams. Here are some common operations:
Reading from a Text File
using System;
using System.IO;
public class Example
{
public static void Main(string[] args)
{
try
{
// Specify the path to the file.
string path = @"C:\Users\Public\Documents\MyFile.txt";
// Create a StreamReader object.
using (StreamReader sr = new StreamReader(path))
{
string line;
// Read line by line.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (Exception e)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}
Writing to a Text File
using System;
using System.IO;
public class Example
{
public static void Main(string[] args)
{
try
{
string path = @"C:\Users\Public\Documents\MyFile.txt";
// Create a StreamWriter object.
using (StreamWriter sw = new StreamWriter(path, append: true)) // append: true to add to the file
{
sw.WriteLine("This is a new line.");
sw.WriteLine("Another line of text.");
}
}
catch (Exception e)
{
Console.WriteLine("The file could not be written:");
Console.WriteLine(e.Message);
}
}
}
Working with Directories
using System;
using System.IO;
public class Example
{
public static void Main(string[] args)
{
string dirPath = @"C:\MyNewDirectory";
// Create a directory
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
Console.WriteLine($"Directory created: {dirPath}");
}
// Get all directories in the current path
string[] directories = Directory.GetDirectories(@"C:\");
Console.WriteLine("\nDirectories in C:\\:");
foreach (string dir in directories)
{
Console.WriteLine(dir);
}
// Get all files in a directory
string[] files = Directory.GetFiles(dirPath);
Console.WriteLine($"\nFiles in {dirPath}:");
if (files.Length == 0)
{
Console.WriteLine("(No files found)");
}
else
{
foreach (string file in files)
{
Console.WriteLine(file);
}
}
}
}