System.IO
Namespace: System.IO
This namespace contains types that allow reading and writing files and data streams, and types that represent and operate on files and directories.
Assembly: mscorlib.dll
Classes
- File Provides static methods and properties to create, copy, delete, move, and open files.
- Directory Provides static methods for the creation, moving, and enumeration of directories and subdirectories.
- Path Exposes static members for creating, manipulating, and retrieving information about paths and file names.
- Stream Abstract base class and a representative of a stream of bytes.
- FileInfo Provides instance methods for the creation, copying, deletion, moving, and opening of files, and aids in their creation.
- DirectoryInfo Provides instance methods for the creation, moving, and enumeration of directories and subdirectories.
- MemoryStream A stream that uses memory to store its contents.
- FileStream Exposes a Stream around a file, supporting both synchronous and asynchronous read and write operations.
- StreamReader Implements a TextReader that reads from a byte-input stream in a particular encoding.
- StreamWriter Implements a TextWriter for writing characters to a stream in a particular encoding.
Structs
- FileAttributes Defines attributes for files and directories.
- FileMode Specifies how the operating system should open a file.
- FileAccess Specifies the constants that define whether a file can be read from or written to.
Enums
- SeekOrigin Specifies the starting point for measuring and comparing the current position of a stream.
Common Usage Example
// Reading from a text file
using (StreamReader sr = new StreamReader("myFile.txt"))
{
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
// Writing to a text file
using (StreamWriter sw = new StreamWriter("myFile.txt", true)) // true to append
{
sw.WriteLine("This is a new line.");
}
// Checking if a file exists
if (File.Exists("myFile.txt"))
{
Console.WriteLine("File exists.");
}