I usually do this when I have to read the whole file always:
using (var fileStream = File.OpenRead(...))
using (var reader = new StreamReader(fileStream))
var content = reader.ReadToEnd();
Any better/faster way?
string content = System.IO.File.ReadAllText(@"your file path");
If you want to get lines:
string[] lines = System.IO.File.ReadAllLines(@"your file path");
if you want to only read lines until get to some line then use the IEnumerable ReadLines:
foreach(string line in System.IO.File.ReadLines(@"your file path")
{
if (line == ...)
{
break;
}
}
精彩评论