Thursday, March 1, 2012

CSharp: How to read a text file


Share at Facebook

In C#, the package System.IO.File will allow you to perform various reading option from a text file.

1) reading the whole content from the file.
string fileContent = System.IO.File.ReadAllText(@"C:\input.txt");
This will read the whole content into the fileContent variable.

2) reading line by line from the text file.
string[] lines = System.IO.File.ReadAllLines(@"C:\input.txt");




No comments: