Feedback

C# - File.ReadLine() without Lock

Veröffentlicht von am 15.01.2019
(0 Bewertungen)
This returns an IEnumerable<string> (something better if the file has many thousand of lines and you only need to parse them one at a time). If you need an array, call it as ReadLines("myfile").ToArray() using LINQ.
public static IEnumerable<string> ReadLines(string path)
{
    using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 0x1000, FileOptions.SequentialScan))
    using (var sr = new StreamReader(fs, Encoding.UTF8))
    {
        string line;
        while ((line = sr.ReadLine()) != null)
        {
            yield return line;
        }
    }
}
Abgelegt unter c#, readfile, file, read, nolock.

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!