In my previous articles I have discussed about the Streaming in .NET and FileStream class in .NET which will give you a fair idea about the streaming concept in .NET framework. In this article I want to discuss about the various ways in which we can read file C# from the File System (backing store).
Read File C# using File Class
- Open – The Open method provided by the File class can be used to read a file. But here we need to take care of the overloaded version of the function. If we are using the below shown Open Methods with two parameters, and If we are passing the FileMode as Append, the file will be opened in Write Access mode for other modes it will be ReadWrite access.
public static FileStream Open(string path, FileMode mode);
But the above scenario is not applicable for the other two overloads(shown below) of the methods
public static FileStream Open(string path, FileMode mode, FileAccess access); public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share);
Below is the code which I have used to read a file from my local system
using (FileStream fs = File.Open("C:\\Test.txt", FileMode.Open)) { byte[] bytesRead = new byte[fs.Length]; fs.Read(bytesRead, 0, Convert.ToInt32(fs.Length)); string result = System.Text.Encoding.UTF8.GetString(bytesRead); }
This is one of the fastest method along with ReadAllBytes(which I will discuss next) to read a file from the system.
- ReadAllBytes – This method can be used to read all the bytes of the File from File system as shown below.
byte[] bytesRead = File.ReadAllBytes("C:\\Test.txt"); string result = System.Text.Encoding.UTF8.GetString(bytesRead);
- ReadAllText – This is the method which is used to read the File’s content as string and not as byte
string text = File.ReadAllText("C:\\Test.txt");
- ReadAllLines – This methods will return the collection of the lines of the file. This is one of the slowest method to retrieve the text of the file as internally it creates a list of lines of files using StreamReader and returns as array of string.
string[] allLines = File.ReadAllLines("C:\\Test.txt");
- OpenText – If we want to get the collection of the lines of the text file just like ReadAllLInes we can also use OpenText which is better in performance then ReadAllLines
StringBuilder sb = new StringBuilder(); using (StreamReader sr = File.OpenText("C:\\Test.txt")) { string s = ""; while ((s = sr.ReadLine()) != null) { sb.Append(s.ToString()); } }
- ReadLines – This method returns the Enumerator over the collection of lines for the file and can be helped to get the collection of lines for the file as shown in the below code snippet.
private static void ReadLines() { StringBuilder sb = new StringBuilder(); foreach (var item in File.ReadLines("C:\\Test.txt")) { sb.Append(item.ToString()); } }
Some Performance Comparisons
While writing about all the methods which can be used to read a file, I was bit curious about the performance of all of these functions though all these methods return different return types but I have converted all the return types to string and tried to measure the performance by reading file of around 15 MB. Please find below table which shows the approximate time taken in milliseconds by each of the method.
Method | Time Taken To Read File(in ms) |
File.Open | 26 |
ReadAllText | 54 |
ReadAllLines | 344 |
OpenText | 124 |
ReadAllBytes | 23 |
ReadLines | 130 |
In this article I have discussed about the various ways in which we can read a file from the system and compared their performance.
Kalyan says
Nice article. This helped me to understand what difference is between all these methods. Comparison of all these methods is very nice.