Now let's check the content of the text file.
Using File.ReadAllText
File.ReadAllText is used to read all the content of the text file in a single string format. File.ReadAllText is effective for reading or getting the content of the small size text file. Now let's understand this with an example. Console.WriteLine("******READ TEXT FILE******");
string filePath = "C:\\Personal\\Codemantra99.com\\Articles\\TesttextFile.txt";
if (File.Exists(filePath))
{
string fileData = File.ReadAllText(filePath);
Console.WriteLine(fileData);
}
else
{
Console.WriteLine("sorry file not found...");
}
Console.ReadLine();
In above code I have first stored the file path in a string variable named as filepath. To validate file path, exist or not I have used File.Exists(filePath). If file does not exist on that case we will display an error message to user. On other hand we will use File.ReadAllText(path) to read the file content in a string variable. After reading the file content I have used Console.WriteLine to display the content of the text file. Now let's run the code to check the output.
Using File.ReadAllLines
File.ReadAllLines is one of the methods which is used to read the text content line by line in the form an array. This methos will help us to read and process each line of code separately. Now let's understand this with an example written in c#.net. Console.WriteLine("******READ TEXT FILE******");
string filePath = "C:\\Personal\\Codemantra99.com\\Articles\\TesttextFile.txt";
if (File.Exists(filePath))
{
string[] fileData = File.ReadAllLines(filePath);
foreach (string item in fileData)
{
Console.WriteLine(item);
}
}
else
{
Console.WriteLine("sorry file not found...");
}
Console.ReadLine();
In above code I have taken the path of the file and validated file exists or not, if it exists on that case I have taken a variable of string type and used File.ReadAllLines to read the text file each line in c#.net. After that I have used for each to apply loop to display each line of the text file. Let's run the code to check the output.
Here in above code, we are able to get the text file each line. Now press F5 to check the output.
Using StreamReader
StreamReader is used to read the text file holding the very large amount of data. This is the most efficient way to read the large size text file contact in c#. StreamReader does not read all the contact of the file at a time it read line by line in the memory. Now let's try to understand it with an example. Console.WriteLine("******READ TEXT FILE******");
string filePath = "C:\\Personal\\Codemantra99.com\\Articles\\TesttextFile.txt";
if (File.Exists(filePath))
{
StreamReader strreader = new StreamReader(filePath);
string linetext;
while ((linetext = strreader.ReadLine()) != null)
{
Console.WriteLine(linetext);
}
}
else
{
Console.WriteLine("sorry file not found...");
}
Console.ReadLine();
In above code I have taken the path of the file and validated the file exists or not. If file exists on that case, I have created object of the StreamReader by passing the file path. In next like I have declared a string variable and used while loop to read each line of the text file. Now let's run the code to check the output.