C# ClassesLesson 35Free preview
C# Files
This lesson is part of C# · All levels
Summary
C# can read and write text files through the File class in the namespace System.IO. Methods such as ReadAllText, WriteAllText, and AppendAllText work directly with the path without the need to manually manage the stream. The file operation still needs to be handled with error because the file may not exist or cannot be written.
Key points
- Read the entire contents of the text file into one string.
- File.WriteAllText writes at once, while AppendAllText adds at the end of the content.
- Supporting method: File.Exists for existence checks and File.Delete for deletion.
- The path is absolute and relatively different; the destination folder must have existed before the file was written.
- Large files should be read per block using StreamReader to save memory.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.