Complete C# Tutorial

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 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 NameWhat It Does (Easy Explanation)
BinaryReaderReads data in binary format (like numbers and text) from a file or stream.
BinaryWriterWrites data in binary format to a file or stream.
BufferedStreamSpeeds up reading and writing by storing data temporarily.
DirectoryHelps create, move, delete, and list folders.
DirectoryInfoGives more details about a folder and allows operations like renaming and deleting.
DriveInfoProvides information about drives (like free space and type of drive).
FileProvides quick ways to create, delete, move, and copy files.
FileInfoGives detailed information about a file and allows changes to it.
FileSystemInfoBase class for FileInfo and DirectoryInfo, provides common properties.
FileStreamReads and writes data to a file, like opening a file in a text editor.
MemoryStreamWorks like a file but stores data in memory instead of on disk.
PathHelps work with file and folder paths, like extracting file names and extensions.
StreamReaderReads text from files easily.
StreamWriterWrites text into files easily.
TextReaderReads text from any input source (base class of StreamReader and StringReader).
TextWriterWrites text to any output source (base class of StreamWriter and StringWriter).
StringReaderReads text from a string, like reading a file but in memory.
StringWriterWrites text into a string, like saving content before writing it to a file.
FileSystemWatcherMonitors 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)
ChangedTriggers when a file is modified (like if someone edits it).
CreatedTriggers when a new file or folder is added.
DeletedTriggers when a file or folder is removed.
RenamedTriggers when a file or folder is renamed.
ErrorTriggers 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)
IOExceptionGeneral file-related error (like reading a locked file).
FileNotFoundExceptionThe file youโ€™re trying to open doesnโ€™t exist.
DirectoryNotFoundExceptionThe folder path is incorrect or missing.
UnauthorizedAccessExceptionNo permission to access the file or folder.
PathTooLongExceptionThe file path or name is too long for the system to handle.
NotSupportedExceptionThe file path format is incorrect or not supported.
EndOfStreamExceptionTrying to read beyond the end of the file.
DriveNotFoundExceptionThe drive (like USB or external hard disk) is missing.
FileLoadExceptionThe file is found but can’t be loaded (maybe corrupted).
InvalidDataExceptionThe file contains invalid or unexpected data.
SecurityExceptionSecurity settings are preventing access to the file.
ArgumentExceptionThe file path contains invalid characters.
ObjectDisposedExceptionYou 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! ๐Ÿš€

Leave a Comment

Share this Doc

System.IO Namespace

Or copy link