Reading Files
File.ReadLines
more efficient than using ReadAllLines for large files.
Returns an IEnumerable<string> of all the lines of code.
IEnumerable<string> enumLines;
string[] lines;
enumLines = System.IO.File.ReadLines(@"C:\\temp\\myfile.txt", Encoding.UTF8);
lines = enumLines.ToArray();
foreach (string line in lines)
{
}
File.ReadAllLines
Added in .NET 4.0
Reads each line into a string array.
string[] lines;
lines = System.IO.File.ReadAllLines(@"C:\\temp\\myfile.txt");
foreach (string line in lines)
{
}
Reads each line into a collection
System.Collections.Generic.List<string> myList;
myList = System.IO.File.ReadAllLines("C:\temp\SavedLists.txt");
File.ReadAllText
Should only be used on very small, basic files
string sWholeFile;
string[] lines;
sWholeFile = System.IO.File.ReadAllText(@"C:\\temp\\myfile.txt");
lines = sWholeFile.Split(Environment.NewLine);
StreamReader.ReadToEnd
System.IO.StreamReader oStreamReader;
string sWholeFile;
oStreamReader = System.IO.StreamReader(@"C:\\temp\\myfile.txt");
sWholeFile = oStreamReader.ReadToEnd;
StreamReader.ReadToEndAsync
System.IO.StreamReader oStreamReader;
string sWholeFile;
oStreamReader = System.IO.StreamReader(@"C:\\temp\\myfile.txt");
using (oStreamReader)
{
sWholeFile = await oStreamReader.ReadToEndAsync();
}
StreamReader.ReadLine
Reads each line into a string array.
System.IO.StreamReader oStreamReader;
string sWholeFile;
oStreamReader = System.IO.StreamReader(@"C:\\temp\\myfile.txt");
while ( (line = oStreamReader.ReadLine() ) != null)
{
}
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext