System.IO Namespace
The System.IO
namespace contains types that allow reading and writing to files and data streams, and provides basic file and directory support.
Classes
File
Provides static methods for the creation, copying, deletion, moving, and opening of files, and helps in the creation of FileStream
objects.
Directory
Exposes static methods for creating, moving, and enumerating through directories and subdirectories.
Path
Provides methods for processing directory strings in a cross-platform manner.
Stream
Represents a sequence of bytes that can be read from, written to, or both.
FileInfo
Provides instance methods for the creation, copying, deletion, moving, and opening of files.
DirectoryInfo
Provides instance methods for creating, moving, and enumerating through directories and subdirectories.
Enums
FileAttributes
– Represents the attributes of a file or directory.FileMode
– Specifies how the operating system should open a file.FileAccess
– Defines the access permissions for a file.FileShare
– Specifies the kind of access other FileStream objects can have to the same file.SeekOrigin
– Defines reference points for seeking within a stream.
Sample Code
using System;
using System.IO;
class Program
{
static void Main()
{
// Write text to a file
File.WriteAllText(@"example.txt", "Hello, System.IO!");
// Read the file back
string content = File.ReadAllText(@"example.txt");
Console.WriteLine(content);
}
}