How to read from a file in C#
This post will discuss how to how to read from a file in C#.
1. Using File Class
A simple solution to read all the text in the file in one go is to use the ReadAllText() method. The following example demonstrates its usage:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; using System.IO; public class Example { public static void Main() { string path = @"C:\file.txt"; string text = File.ReadAllText(path); Console.WriteLine(text); } } |
The File.ReadAllLines() method read all the lines of the specified file into a string array. It can be used as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; using System.IO; public class Example { public static void Main() { string path = @"C:\file.txt"; string[] lines = File.ReadAllLines(path); Console.WriteLine(String.Join(Environment.NewLine, lines)); } } |
You should not use the above methods with large files. The ReadLines() method is preferred over the ReadAllLines() method as ReadLines returns an Enumerable, and you can enumerate before the whole collection is returned.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; using System.Collections.Generic; using System.IO; public class Example { public static void Main() { string path = @"C:\file.txt"; IEnumerable<string> lines = File.ReadLines(path); Console.WriteLine(String.Join(Environment.NewLine, lines)); } } |
2. Using FileStream Class
For large files, you can use the FileStream.Read() method to read the whole file in chunks. Here’s an example of how you could achieve that.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
using System; using System.Text; using System.IO; public class Example { public static void Main() { string path = @"C:\file.txt"; using (FileStream stream = File.OpenRead(path)) { int totalBytes = (int)stream.Length; byte[] bytes = new byte[totalBytes]; int bytesRead = 0; while (bytesRead < totalBytes) { int len = stream.Read(bytes, bytesRead, totalBytes); bytesRead += len; } string text = Encoding.UTF8.GetString(bytes); Console.WriteLine(text); } } } |
3. Using StreamReader Class
Another option is to use the StreamReader.ReadLine() method. You can use it to read lines from a file until the end of the file is reached.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using System.IO; public class Example { public static void Main() { string path = @"C:\file.txt"; using (StreamReader reader = new StreamReader(path)) { string line; while ((line = reader.ReadLine()) != null) { Console.WriteLine(line); } } } } |
For small files, you can use the StreamReader.ReadToEnd() method, which reads the whole file in a single operation.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; using System.IO; public class Example { public static void Main() { string path = @"C:\file.txt"; using (StreamReader streamReader = new StreamReader(path)) { string text = streamReader.ReadToEnd(); Console.WriteLine(text); } } } |
That’s all about reading from a file in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)