Argus.IO.ReverseFileReader 2019.11.22.1

A file/stream reader that is designed to iterate over a file or a stream line by line in reverse order in a way that does not read all of the lines into memory at one time.  This supports .NET Standard, the full framework as well as the Windows Universal Platform (UWP) apps.

There is a newer version of this package available.
See the version list below for details.
Install-Package Argus.IO.ReverseFileReader -Version 2019.11.22.1
dotnet add package Argus.IO.ReverseFileReader --version 2019.11.22.1
<PackageReference Include="Argus.IO.ReverseFileReader" Version="2019.11.22.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Argus.IO.ReverseFileReader --version 2019.11.22.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Argus.IO.ReverseFileReader, 2019.11.22.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Argus.IO.ReverseFileReader as a Cake Addin
#addin nuget:?package=Argus.IO.ReverseFileReader&version=2019.11.22.1

// Install Argus.IO.ReverseFileReader as a Cake Tool
#tool nuget:?package=Argus.IO.ReverseFileReader&version=2019.11.22.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Reverse File/Stream Reader

NuGet version (ReverseFileReader) NuGet version (ReverseFileReader)

A file/stream reader that is designed to iterate over a file or a stream line by line in reverse order in a way that does not read all of the lines into memory at one time. This supports .NET Standard, the full framework as well as the Windows Universal Platform (UWP) apps.

Although this is geared towards files it will read any Stream line by line in reverse order.

.Net Framework Support

  • .NET Standard 2.1
  • .NET Standard 2.0
  • .NET Framework 4.7.2
  • .NET Framework 4.7.1
  • .NET Framework 4.7
  • .NET Framework 4.6.2
  • .NET Framework 4.6.1
  • .NET Framework 4.6
  • .NET Framework 4.5.2
  • .NET Framework 4.5.1
  • .NET Framework 4.5
  • .NET Framework 4

Read a file with CrLf line endings

C#
var sb = new StringBuilder();

using (var reader = new Argus.IO.ReverseFileReader(@"C:\Temp\log-file.txt"))
{
    while (!reader.StartOfFile)
    {
        sb.AppendLine(reader.ReadLine());
    }
}

Read a file with Lf line endings (Cr also supported with a change to the LineEnding property)

C#
var sb = new StringBuilder();

using (var reader = new Argus.IO.ReverseFileReader(@"C:\Temp\log-file.txt"))
{
    reader.LineEnding = Argus.IO.LineEnding.Lf;

    while (!reader.StartOfFile)
    {
        sb.AppendLine(reader.ReadLine());
    }
}

Windows Universal Platform Example (UWP)

The FileStream is available in UWP but for all intensive purposes is unusable due to the apps being sandboxed. If you instantiate the ReverseFileReader with a file path it will likely end with a permissions exception. Instead, you'll get a StorageFile and then pass its Stream off to the ReverseFileReader which will work.

One thing to note that could cause a related issue is that since Windows 10 build 14393 the TextBox and RichEditBox use only a carriage return (when those save they only save with a carriage return unless you intervene). Pay attention to that if it becomes an issue. In the near future I will provide an auto-detect method that will attempt to infer what line endings the file is using.

C#
/// <summary>
/// Opens a document and puts the contents into a TextBox named TextBoxMain.
/// </summary>
/// <returns></returns>
private async Task OpenDocument()
{
    var picker = new Windows.Storage.Pickers.FileOpenPicker
    {
        SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary
    };

    picker.FileTypeFilter.Add(".txt");
    var OpenStorageFile = await picker.PickSingleFileAsync();

    if (OpenStorageFile != null)
    {
        var sb = new StringBuilder();

        using (var stream = await OpenStorageFile.OpenStreamForReadAsync())
        {
            using (var reader = new Argus.IO.ReverseFileReader(stream))
            {
                reader.LineEnding = Argus.IO.LineEnding.CrLf;

                while (!reader.StartOfFile)
                {
                    sb.AppendLine(reader.ReadLine());
                }

            }
        }

        TextBoxMain.Text = sb.ToString();
        return;
    }
}

Tail

I have provided a console program that is a lightweight tail utility. It reads lines of the end of the provided file. If more lines are requested than the file has the entire file is returned.

Syntax: tail <filename>
        tail <filename> <number of lines to fetch>

Reverse File/Stream Reader

NuGet version (ReverseFileReader) NuGet version (ReverseFileReader)

A file/stream reader that is designed to iterate over a file or a stream line by line in reverse order in a way that does not read all of the lines into memory at one time. This supports .NET Standard, the full framework as well as the Windows Universal Platform (UWP) apps.

Although this is geared towards files it will read any Stream line by line in reverse order.

.Net Framework Support

  • .NET Standard 2.1
  • .NET Standard 2.0
  • .NET Framework 4.7.2
  • .NET Framework 4.7.1
  • .NET Framework 4.7
  • .NET Framework 4.6.2
  • .NET Framework 4.6.1
  • .NET Framework 4.6
  • .NET Framework 4.5.2
  • .NET Framework 4.5.1
  • .NET Framework 4.5
  • .NET Framework 4

Read a file with CrLf line endings

C#
var sb = new StringBuilder();

using (var reader = new Argus.IO.ReverseFileReader(@"C:\Temp\log-file.txt"))
{
    while (!reader.StartOfFile)
    {
        sb.AppendLine(reader.ReadLine());
    }
}

Read a file with Lf line endings (Cr also supported with a change to the LineEnding property)

C#
var sb = new StringBuilder();

using (var reader = new Argus.IO.ReverseFileReader(@"C:\Temp\log-file.txt"))
{
    reader.LineEnding = Argus.IO.LineEnding.Lf;

    while (!reader.StartOfFile)
    {
        sb.AppendLine(reader.ReadLine());
    }
}

Windows Universal Platform Example (UWP)

The FileStream is available in UWP but for all intensive purposes is unusable due to the apps being sandboxed. If you instantiate the ReverseFileReader with a file path it will likely end with a permissions exception. Instead, you'll get a StorageFile and then pass its Stream off to the ReverseFileReader which will work.

One thing to note that could cause a related issue is that since Windows 10 build 14393 the TextBox and RichEditBox use only a carriage return (when those save they only save with a carriage return unless you intervene). Pay attention to that if it becomes an issue. In the near future I will provide an auto-detect method that will attempt to infer what line endings the file is using.

C#
/// <summary>
/// Opens a document and puts the contents into a TextBox named TextBoxMain.
/// </summary>
/// <returns></returns>
private async Task OpenDocument()
{
    var picker = new Windows.Storage.Pickers.FileOpenPicker
    {
        SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary
    };

    picker.FileTypeFilter.Add(".txt");
    var OpenStorageFile = await picker.PickSingleFileAsync();

    if (OpenStorageFile != null)
    {
        var sb = new StringBuilder();

        using (var stream = await OpenStorageFile.OpenStreamForReadAsync())
        {
            using (var reader = new Argus.IO.ReverseFileReader(stream))
            {
                reader.LineEnding = Argus.IO.LineEnding.CrLf;

                while (!reader.StartOfFile)
                {
                    sb.AppendLine(reader.ReadLine());
                }

            }
        }

        TextBoxMain.Text = sb.ToString();
        return;
    }
}

Tail

I have provided a console program that is a lightweight tail utility. It reads lines of the end of the provided file. If more lines are requested than the file has the entire file is returned.

Syntax: tail <filename>
        tail <filename> <number of lines to fetch>

Release Notes

- Updated targets to add .NET Standard 2.1

  • .NETFramework 4.0

    • No dependencies.
  • .NETFramework 4.5

    • No dependencies.
  • .NETFramework 4.5.1

    • No dependencies.
  • .NETFramework 4.5.2

    • No dependencies.
  • .NETFramework 4.6

    • No dependencies.
  • .NETFramework 4.6.1

    • No dependencies.
  • .NETFramework 4.6.2

    • No dependencies.
  • .NETFramework 4.7

    • No dependencies.
  • .NETFramework 4.7.1

    • No dependencies.
  • .NETFramework 4.7.2

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
2020.11.11.1 277 11/11/2020
2019.11.22.1 238 11/22/2019
2019.4.7.1 317 4/7/2019