VersaTul.Data.FileReader 1.0.6

Prefix Reserved
dotnet add package VersaTul.Data.FileReader --version 1.0.6
                    
NuGet\Install-Package VersaTul.Data.FileReader -Version 1.0.6
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="VersaTul.Data.FileReader" Version="1.0.6" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="VersaTul.Data.FileReader" Version="1.0.6" />
                    
Directory.Packages.props
<PackageReference Include="VersaTul.Data.FileReader" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add VersaTul.Data.FileReader --version 1.0.6
                    
#r "nuget: VersaTul.Data.FileReader, 1.0.6"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package VersaTul.Data.FileReader@1.0.6
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=VersaTul.Data.FileReader&version=1.0.6
                    
Install as a Cake Addin
#tool nuget:?package=VersaTul.Data.FileReader&version=1.0.6
                    
Install as a Cake Tool

VersaTul Data.FileReader

VersaTul.Data.FileReader is a small library that provides simple, testable file-reading helpers to produce IDataReader instances from common file formats. It focuses on CSV and plain text files and exposes a thin abstraction so callers can easily obtain forward-only readers that integrate with other data-processing or bulk-copy components.

Features

  • Read a single file (CSV or text) and obtain an IDataReader.
  • Enumerate and read files from a directory based on extension filters.
  • Pluggable readers via ICsvReader and ITextReader interfaces so you can replace or mock readers in tests.
  • Supports options such as whether CSV files contain a header row via IOptions / FileOptions.
  • Returns readers wrapped so underlying streams are disposed when the reader is disposed.

Key types

  • IFileReader - high-level API for reading a single file or all files in a directory:

    • IDataReader? Read(string directoryPath, string filename, IOptions options)
    • IEnumerable<IDataReader> Read(string directoryPath, IOptions options)
  • ICsvReader - CSV-specific reader that returns an IDataReader for a given file path and options.

  • ITextReader - Plain-text reader that returns an IDataReader for a given file path and options.

  • IOptions / FileOptions - Options to control reading behavior (for example HasHeader and ExtensionFilters).

  • DisposableDataReader - wrapper that ensures both the returned reader and its underlying stream are disposed together.

Installation

This project is typically consumed as part of the larger VersaTul solution. If distributed as a NuGet package, install it using:

dotnet add package VersaTul.Data.FileReader

Quick usage

Read a single CSV file:

var fileReader = new DataFileReader(fileUtility, csvReader, textReader);
var options = new FileOptions { HasHeader = true };

using var reader = fileReader.Read("C:\\path\\to", "file.csv", options);
if (reader != null)
{
    while (reader.Read())
    {
        // process columns via reader.GetValue(i) or reader.GetString(i)
    }
}

Read all CSV and text files from a directory (respecting options.ExtensionFilters):

var options = new FileOptions { HasHeader = true, ExtensionFilters = new [] { ExtensionFilters.CSV, ExtensionFilters.TEXT } };
var readers = fileReader.Read("C:\\path\\to", options);

foreach (var r in readers)
{
    using (r)
    {
        while (r.Read()) { /* process row */ }
    }
}

Notes:

  • DataFileReader delegates actual parsing to the registered ICsvReader/ITextReader implementations so you can swap in different CSV parsers or mocks for testing.
  • CsvFileReader in this project uses LumenWorks.Framework.IO.Csv internally and returns an IDataReader that disposes the underlying stream when the reader is disposed.

Dependency injection

When using an IoC container you can register the concrete types used by the host. Example using Autofac (similar to the AppModule used in the sample host project):

builder.RegisterType<FileUtility>().As<IFileUtility>().SingleInstance();
builder.RegisterType<CsvFileReader>().As<ICsvReader>().SingleInstance();
builder.RegisterType<TextFileReader>().As<ITextReader>().SingleInstance();
builder.RegisterType<DataFileReader>().As<IFileReader>().SingleInstance();

Example using Microsoft.Extensions.DependencyInjection:

services.AddSingleton<IFileUtility, FileUtility>();
services.AddSingleton<ICsvReader, CsvFileReader>();
services.AddSingleton<ITextReader, TextFileReader>();
services.AddSingleton<IFileReader, DataFileReader>();

Extensibility

  • To support additional file formats add a reader that implements IReader (or a format-specific interface) and update DataFileReader registration to include it.
  • FileOptions.ExtensionFilters drives directory scanning; add and handle new ExtensionFilters values when extending supported formats.

Examples (additional)

1) Print column headings and first N rows (similar to host command)

var options = new FileOptions { HasHeader = true };
using var reader = fileReader.Read("C:\\data", "customers.csv", options);

if (reader != null)
{
    // Print column names
    var fieldCount = reader.FieldCount;
    for (int i = 0; i < fieldCount; i++)
        Console.Write(reader.GetName(i) + (i + 1 < fieldCount ? ", " : "\n"));

    int row = 0;
    while (reader.Read() && row < 25)
    {
        for (int i = 0; i < fieldCount; i++)
            Console.Write(reader.GetValue(i) + (i + 1 < fieldCount ? " | " : "\n"));
        row++;
    }
}

2) Using DataFileReader with a bulk copy operation

var options = new FileOptions { HasHeader = true };
using var reader = fileReader.Read("C:\\data", "people.csv", options);

var mappings = new List<IBulkCopyColumnMapping> {
    new BulkCopyColumnMapping<Person, Person>(p => p.Name, p => p.Name),
    new BulkCopyColumnMapping<Person, Person>(p => p.Age, p => p.Age)
};

var copyDetail = new CopyDetail("Persons", reader, mappings);
bulkCopy.DoCopy(copyDetail);

Note: CopyDetail constructors in this solution expect an IDataReader for source data. Ensure the reader is disposed after the bulk operation completes.

3) Unit test: mock ICsvReader to return a controlled IDataReader

var mockCsv = new Mock<ICsvReader>();
var mockOptions = new FileOptions { HasHeader = true };

// Build a simple DataTable and return its reader
var dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Rows.Add(1, "Alice");

mockCsv.Setup(c => c.Read(It.IsAny<string>(), It.IsAny<IOptions>())).Returns(dt.CreateDataReader());

var fileReader = new DataFileReader(fileUtilityMock.Object, mockCsv.Object, textReaderMock.Object);
var reader = fileReader.Read("C:\\path", "file.csv", mockOptions);
Assert.NotNull(reader);

4) Read a directory but only process CSV files

var options = new FileOptions { HasHeader = true, ExtensionFilters = new[] { ExtensionFilters.CSV } };
var readers = fileReader.Read("C:\\data", options);

foreach (var r in readers)
{
    using (r)
    {
        // process each CSV file returned by the directory scan
    }
}

License

This project follows the same license as the VersaTul solution. See the repository LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.6 396 11/17/2025
1.0.5 308 11/17/2025
1.0.3 302 11/17/2025
1.0.2 317 11/17/2025