How do you read and write files in C#?
π‘ Concept: File Reading and Writing
File handling in C# involves reading data from files and writing data to files using various classes and methods.
π Quick Intro
C# provides multiple APIs like StreamReader
, StreamWriter
, and File
class to facilitate file I/O operations.
π§ Analogy
Think of file handling like using a pen and paper β you can read whatβs written or write new content.
π§ Technical Explanation
- π
StreamReader
reads text from a file sequentially. - βοΈ
StreamWriter
writes text to a file, creating or overwriting it. - π The
File
class provides static methods for quick file operations. - βοΈ Supports synchronous and asynchronous methods for better performance.
- π οΈ Proper disposal of streams is important to release resources.
π― Use Cases
- β Reading configuration files.
- β Logging application data.
- β Processing large text files.
- β Saving user data or reports.
π» Code Example
using System;
using System.IO;
class Program {
static void Main() {
// Writing to a file
using (StreamWriter writer = new StreamWriter(""example.txt"")) {
writer.WriteLine(""Hello, World!"");
}
// Reading from a file
using (StreamReader reader = new StreamReader(""example.txt"")) {
string content = reader.ReadToEnd();
Console.WriteLine(content);
}
}
}

β Interview Q&A
Q1: What class is used to read text files?
A: StreamReader.
Q2: How to write text to a file?
A: Using StreamWriter or File.WriteAllText.
Q3: How to ensure files are properly closed?
A: Use 'using' statement or call Dispose.
Q4: Can file operations be asynchronous?
A: Yes, using async methods.
Q5: What namespace contains file handling classes?
A: System.IO.
Q6: What happens if a file doesn't exist?
A: Reading throws exception; writing creates new file.
Q7: How to append text to an existing file?
A: Use StreamWriter with append parameter.
Q8: What is the difference between StreamReader and File.ReadAllText?
A: StreamReader reads streams; File.ReadAllText reads all content at once.
Q9: Can you read binary files with these classes?
A: No, use BinaryReader for binary data.
Q10: How to handle large files efficiently?
A: Read line by line or use buffering.
π MCQs
Q1. What class reads text files?
- StreamWriter
- File
- StreamReader
- BinaryReader
Q2. How to write text to file?
- StreamReader
- StreamWriter or File.WriteAllText
- File.ReadAllText
- Console.Write
Q3. How to ensure files close?
- 'using' statement
- Close()
- Dispose()
- Flush()
Q4. Can file ops be async?
- No
- Yes
- Sometimes
- Never
Q5. Namespace for file handling?
- System.Net
- System.IO
- System.Text
- System.Threading
Q6. What if file missing?
- Creates file
- Exception when reading
- Deletes file
- Ignores
Q7. How to append text?
- StreamReader append
- StreamWriter append
- File.ReadAllText append
- Console.Write
Q8. StreamReader vs File.ReadAllText?
- Same
- StreamReader reads streams
- File.ReadAllText reads streams
- None
Q9. Read binary files?
- Yes
- No
- Sometimes
- Always
Q10. Handle large files?
- Read all at once
- Read line by line
- Ignore
- Skip
π‘ Bonus Insight
Efficient file I/O is crucial for performance, especially with large datasets in C# applications.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!