JavaScript required
We’re sorry, but Coda doesn’t work properly without JavaScript enabled.
Skip to content
Gallery
Study Notes(Edit)
Resources
Networks
CAB201
Binary Calculator
Color Palette Finder
Database Management
More
Share
Explore
Exam Notes
Week 4: Files, Directories, and I/O in C#
🔹 1. File Handling Basics
Key Classes for File Operations:
File
(high-level): Provides easy, static methods for quick file operations.
Common Methods
:
File.Exists("path")
: Checks if a file exists.
File.Delete("path")
: Deletes the file.
File.Copy("source", "destination")
: Copies a file to a new location.
FileStream
(low-level): Allows byte-level control over file access and operations.
🔹 2. Reading and Writing Files
🔸 Reading from Files
File.ReadAllText("path")
: Reads the entire file content as a string.
StreamReader
: Reads text files line-by-line, useful for large files.
Example
:
csharp
Copy code
using (StreamReader reader = new StreamReader("file.txt"))
{
while (!reader.EndOfStream)
{
Console.WriteLine(reader.ReadLine());
}
}
🔸 Writing to Files
File.WriteAllText("path", "text")
: Writes text to a file, overwriting existing content.
File.AppendAllText("path", "text")
: Appends text to an existing file.
StreamWriter
: Writes to a file, line-by-line if needed.
Example
:
csharp
Copy code
using (StreamWriter writer = new StreamWriter("file.txt"))
{
writer.WriteLine("Hello, World!");
}
🔹 3. Directory Management
Checking Directory Existence
:
Directory.Exists("path")
Creating a Directory
:
Directory.CreateDirectory("path")
Getting Directory Metadata
: Use
DirectoryInfo
for details like creation and modification dates.
Example
:
csharp
Copy code
DirectoryInfo info = new DirectoryInfo("path");
Console.WriteLine(info.CreationTime);
🔹 4. Path Operations
Path Class
: Simplifies file and directory path management.
Common Methods
:
Path.Combine("folder", "file.txt")
: Combines folder and file names into a full path.
Path.GetExtension("file.txt")
: Returns the file extension, e.g.,
.txt
Path.GetFileNameWithoutExtension("file.txt")
: Returns the file name without extension, e.g.,
file
🔹 5. Working Directory
Getting Current Directory
:
Environment.CurrentDirectory
: Returns the current directory path.
Directory.GetCurrentDirectory()
: Also retrieves the current working directory.
🔹 6. Exception Handling in File Operations
Common Exceptions
FileNotFoundException
: Triggered when trying to read a non-existent file.
DirectoryNotFoundException
: Triggered if a specified directory does not exist.
IOException
: A general I/O exception, often caused by access conflicts.
Best Practice: Always handle exceptions in file I/O to prevent crashes.
Example
:
csharp
Copy code
try
{
string text = File.ReadAllText("file.txt");
}
catch (FileNotFoundException e)
{
Console.WriteLine("File not found.");
}
🔹 7. Using
using
Statement for Resource Management
Purpose
: Ensures proper disposal of resources (like file streams) after use.
Syntax
:
csharp
Copy code
using (StreamReader reader = new StreamReader("file.txt"))
{
// Perform file reading
} // StreamReader is automatically closed here
🔹 8. Summary of Key Classes and Methods
Table 16
Table 16
Class
Purpose
Common Methods
Class
Purpose
Common Methods
File
High-level file management
Exists
,
ReadAllText
,
WriteAllText
FileStream
Low-level byte-based file operations
Open, Close
StreamReader
Text-based file reading
ReadLine
,
ReadToEnd
StreamWriter
Text-based file writing
Write
,
WriteLine
Directory
High-level directory management
Exists
,
CreateDirectory
DirectoryInfo
Directory metadata
Creation time, last access time
Path
Path management helper
Combine
,
GetExtension
,
GetFileName
There are no rows in this table
✨ Quick Tips for Exam Prep
Remember
using
statement
: Essential for managing resources automatically.
Know the key classes
:
High-level (easier):
File
,
Directory
Low-level (more control):
FileStream
Common Exceptions
:
FileNotFoundException
,
DirectoryNotFoundException
,
IOException
— make sure to practice handling these.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
Ctrl
P
) instead.