System.IO Namespace in C# - File Handling Made Easy
๐ Hey there! Ready to explore System.IO?
Imagine you want to store user data, logs, or game progress in your C# program. How do you do it? ๐ค You canโt just store everything in variablesโthey disappear when the program closes! Thatโs where System.IO comes in.
The System.IO namespace in C# provides classes and methods to work with files, directories, and streams. It helps you create, read, write, move, and delete files effortlessly. ๐
๐ฏ What You Are Going to Learn in This Lesson:
โ๏ธ Understand what the System.IO namespace is and why it’s important.
โ๏ธ List of System.IO namespace: Classes, Methods, Properties, Events, and Exceptions.
๐ What is System.IO Namespace?
The System.IO namespace in C# is like your personal toolbox for handling files and data streams.
๐ ๏ธ It provides classes to create, read, write, and manage files and directories on your system. Whether you need to save a document, read a text file, or manage folders, System.IO has got you covered! It includes powerful classes like File, Directory, StreamReader, StreamWriter, TextReader, TextWriter, and many more.
๐ Youโll use this namespace in real-world applications like logging systems, data storage, and file transfers. Stay tuned, because weโll explore these in detail with fun and easy examples! ๐
The System.IO namespace provides classes and methods to handle files and directories in C#. With it, you can:
- ๐ Work with files โ Create, read, write, append, and delete files.
- ๐ Manage directories โ Create and delete folders.
- ๐ Read & write data streams โ Handle large data efficiently.
It contains several useful classes that make file handling easy. Letโs explore them! ๐
๐ Common and Popular Classes in System.IO
Here are the most important classes in System.IO:
Class Name | What It Does (Easy Explanation) |
---|---|
BinaryReader | Reads data in binary format (like numbers and text) from a file or stream. |
BinaryWriter | Writes data in binary format to a file or stream. |
BufferedStream | Speeds up reading and writing by storing data temporarily. |
Directory | Helps create, move, delete, and list folders. |
DirectoryInfo | Gives more details about a folder and allows operations like renaming and deleting. |
DriveInfo | Provides information about drives (like free space and type of drive). |
File | Provides quick ways to create, delete, move, and copy files. |
FileInfo | Gives detailed information about a file and allows changes to it. |
FileSystemInfo | Base class for FileInfo and DirectoryInfo, provides common properties. |
FileStream | Reads and writes data to a file, like opening a file in a text editor. |
MemoryStream | Works like a file but stores data in memory instead of on disk. |
Path | Helps work with file and folder paths, like extracting file names and extensions. |
StreamReader | Reads text from files easily. |
StreamWriter | Writes text into files easily. |
TextReader | Reads text from any input source (base class of StreamReader and StringReader). |
TextWriter | Writes text to any output source (base class of StreamWriter and StringWriter). |
StringReader | Reads text from a string, like reading a file but in memory. |
StringWriter | Writes text into a string, like saving content before writing it to a file. |
FileSystemWatcher | Monitors folders and files for any changes (like new files or deletions). |
๐ Common Properties in System.IO
Properties help get useful details about files, folders, and drives.
Class Name | Property Name | What It Does (Easy Explanation) |
---|---|---|
FileInfo |
Length |
Shows the file size (in bytes). |
FileInfo |
DirectoryName |
Tells which folder the file is inside. |
DirectoryInfo |
Parent |
Shows the parent folder (where the current folder is located). |
DirectoryInfo |
Root |
Tells the main root folder (like C:\ on Windows). |
DriveInfo |
AvailableFreeSpace |
Shows how much free space is left. |
DriveInfo |
DriveType |
Tells if it’s a Hard Drive, SSD, or USB. |
DriveInfo |
IsReady |
Checks if the drive is connected. |
Path |
DirectorySeparatorChar |
Shows the slash (/ or \) used in file paths. |
Path |
PathSeparator |
Shows the character used to separate paths in system variables. |
FileSystemInfo |
Exists |
Checks if a file or folder exists. |
FileSystemInfo |
FullName |
Returns the full path of a file or folder. |
๐ Common Methods in System.IO
Methods allow you to perform actions like creating, deleting, or modifying files, folders, and text.
Class Name | Method Name | What It Does (Easy Explanation) |
---|---|---|
FileInfo |
Create() |
Makes a new empty file. |
Delete() |
Removes a file forever. | |
CopyTo("newPath") |
Copies a file to another location. | |
MoveTo("newPath") |
Moves a file to another folder. | |
DirectoryInfo |
Create() |
Makes a new folder. |
Delete(true/false) |
Deletes a folder (if true, deletes everything inside too). | |
GetFiles() |
Lists all files inside a folder. | |
GetDirectories() |
Lists all subfolders inside a folder. | |
DriveInfo |
GetDrives() |
Lists all available drives on the system. |
Path |
Combine("folder", "file.txt") |
Joins folder and file names into a full path. |
|
GetExtension("file.txt") |
Gets the file extension (.txt, .jpg, etc.). |
GetFileName("C:\Docs\file.txt") |
Extracts just the file name (file.txt). | |
StreamReader |
ReadToEnd() |
Reads the whole file as text. |
StreamWriter |
Write("Hello!") |
Writes text into a file. |
TextReader |
ReadLine() |
Reads one line of text. |
TextWriter |
WriteLine("Hello!") |
Writes text with a new line. |
StringReader |
ReadToEnd() |
Reads the entire string content. |
StringWriter |
ToString() |
Returns the written content as a string. |
๐ข Events in System.IO (FileSystemWatcher)
Events help you track changes in folders and files.
Event Name | What It Does (Easy Explanation) |
---|---|
Changed | Triggers when a file is modified (like if someone edits it). |
Created | Triggers when a new file or folder is added. |
Deleted | Triggers when a file or folder is removed. |
Renamed | Triggers when a file or folder is renamed. |
Error | Triggers when an error happens (like if a file is suddenly locked). |
๐จ Common Exceptions in File Handling (C#)
When working with files in C#, you might run into various exceptions. Below is a list of common file handling exceptions, along with simple explanations.
Exception Name | What It Means (Easy Explanation) |
---|---|
IOException | General file-related error (like reading a locked file). |
FileNotFoundException | The file youโre trying to open doesnโt exist. |
DirectoryNotFoundException | The folder path is incorrect or missing. |
UnauthorizedAccessException | No permission to access the file or folder. |
PathTooLongException | The file path or name is too long for the system to handle. |
NotSupportedException | The file path format is incorrect or not supported. |
EndOfStreamException | Trying to read beyond the end of the file. |
DriveNotFoundException | The drive (like USB or external hard disk) is missing. |
FileLoadException | The file is found but can’t be loaded (maybe corrupted). |
InvalidDataException | The file contains invalid or unexpected data. |
SecurityException | Security settings are preventing access to the file. |
ArgumentException | The file path contains invalid characters. |
ObjectDisposedException | You are trying to use a file that has already been closed. |
๐ Conclusion
Congratulations! ๐ Youโve just explored the System.IO namespace in C# and got a glimpse of how it helps in file handling, directories, and streams. ๐
We covered various classes, methods, properties, events, and exceptions that make it easier to work with files and data streams. These tools are crucial for building real-world applications like logging systems, data storage, and document management.
But donโt worry if it feels like a lot! ๐คฏ Youโll learn each of these in detail in the upcoming chapters, with hands-on examples to make things fun and easy. ๐
ย
๐ Next What?
Now that you understand System.IO, itโs time to get practical! In the next chapter, youโll learn how to create files in C# file handling. ๐โ๏ธ
So, get ready to start working with actual files! See you there! ๐