How to read a text file reversely with iterator in C#

后端 未结 11 1860
甜味超标
甜味超标 2020-11-22 04:05

I need to process a large file, around 400K lines and 200 M. But sometimes I have to process from bottom up. How can I use iterator (yield return) here? Basically I don\'t l

11条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 04:25

    Very fast solution for huge files. Use powershell Get-Content cmdlet with Tail option. Calling powershell will give a little bit overhead, but for huge files its worthless

    using System.Management.Automation;
    
    const string FILE_PATH = @"d:\temp\b_media_27_34_0000_25393.txt";
    var ps = PowerShell.Create();
    ps.AddCommand("Get-Content")
        .AddParameter("Path", FILE_PATH)
        .AddParameter("Tail", 1);
    var psResults = ps.Invoke();
    var lastLine = psResults.FirstOrDefault()?.BaseObject.ToString();
    
    ps.Dispose();
    

    Required powershell reference

    C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll

提交回复
热议问题