Oakrey.Applications.Files 3.0.2

dotnet add package Oakrey.Applications.Files --version 3.0.2
                    
NuGet\Install-Package Oakrey.Applications.Files -Version 3.0.2
                    
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="Oakrey.Applications.Files" Version="3.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Oakrey.Applications.Files" Version="3.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Oakrey.Applications.Files" />
                    
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 Oakrey.Applications.Files --version 3.0.2
                    
#r "nuget: Oakrey.Applications.Files, 3.0.2"
                    
#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 Oakrey.Applications.Files@3.0.2
                    
#: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=Oakrey.Applications.Files&version=3.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Oakrey.Applications.Files&version=3.0.2
                    
Install as a Cake Tool

Oakrey.Applications.Files

A .NET 10 class library that provides file and directory management through a single injectable service interface. It supports synchronous and asynchronous operations, thread-safe file access via SemaphoreSlim, age-based cleanup, and full OpenTelemetry tracing and structured logging on every operation.

Main features

  • Read file content as a string, synchronously or asynchronously with CancellationToken support
  • Read and write files via FileStream
  • Save text content to a file with automatic intermediate directory creation
  • Create directories on demand
  • Delete a single file by path
  • Delete files in a directory that are older than a given TimeSpan, with optional extension filter
  • Enumerate all file paths in a directory tree by extension
  • Thread-safe async load and save backed by SemaphoreSlim
  • Every operation emits an OpenTelemetry activity span and structured log entries via Oakrey.Log

Architecture

classDiagram
    class IFileService {
        +CreateFolder(folderName)
        +CreateSaveStream(fileName) FileStream
        +DeleteFile(fileName)
        +DeleteFileOlderThan(path, timeSpan, extension, ct) Task
        +GetFilesPaths(folder, extension) string[]
        +LoadFile(fileName) string
        +LoadFileAsync(fileName, ct) Task~string~
        +LoadFileStream(fileName) FileStream
        +SaveFile(fileName, value)
    }

    class FileService {
        -ILogger logger
        -ITracing tracer
        -SemaphoreSlim semaphoreSlim
        +FileService()
    }

    IFileService <|.. FileService

Requirements

  • .NET 10 or higher
  • Oakrey.Log 2.0.0 or higher
  • Oakrey.Telemetry 2.0.1 or higher

Installation

NuGet Package Manager

  1. In Visual Studio open Tools > NuGet Package Manager > Manage NuGet Packages for Solution.
  2. Search for Oakrey.Applications.Files and click Install.

.NET CLI

dotnet add package Oakrey.Applications.Files

Package Manager Console

Install-Package Oakrey.Applications.Files

Configuration

FileService has no required configuration. Register it with the DI container of your choice:

// Microsoft.Extensions.DependencyInjection
services.AddSingleton<IFileService, FileService>();

Oakrey.Log and Oakrey.Telemetry must be configured separately according to their own documentation before FileService is resolved.

Example usage

public class ReportExporter(IFileService fileService)
{
    // Save a text report, directories are created automatically
    public async Task SaveReportAsync(string path, string content, CancellationToken ct)
    {
        fileService.SaveFile(path, content);
    }

    // Load a text report asynchronously
    public async Task<string> LoadReportAsync(string path, CancellationToken ct)
    {
        return await fileService.LoadFileAsync(path, ct);
    }

    // Write binary output via stream
    public FileStream OpenWriteStream(string path)
    {
        return fileService.CreateSaveStream(path);
    }

    // Remove log files older than 30 days
    public Task CleanupLogsAsync(string logDirectory, CancellationToken ct)
    {
        return fileService.DeleteFileOlderThan(logDirectory, TimeSpan.FromDays(30), "log", ct);
    }

    // List all CSV files in a directory tree
    public string[] GetCsvFiles(string root)
    {
        return fileService.GetFilesPaths(root, "csv");
    }
}

Development notes

  • SaveFile and LoadFileAsync share a single SemaphoreSlim(1,1) instance to prevent concurrent read/write collisions on the same FileService instance. Register the service as a singleton to benefit from this guarantee.
  • DeleteFileOlderThan uses file.LastWriteTime for age comparison and respects CancellationToken between file deletions.
  • Every public method wraps its body in a try/catch, logs the error via Oakrey.Log, and re-throws so callers receive the original exception with full context.
  • All operations start an OpenTelemetry Activity via Oakrey.Telemetry.Tracing; configure an exporter in the host to capture traces.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Author: Oakrey
Repository: https://dev.azure.com/oakrey/OpenPackages/_git/ApplicationServices
Project URL: http://www.oakrey.cz/opkg_applications

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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 (1)

Showing the top 1 NuGet packages that depend on Oakrey.Applications.Files:

Package Downloads
Oakrey.Applications.Json

Provides IJsonService for stream-based JSON serialization and deserialization using System.Text.Json, with async CancellationToken support, batch directory load, IName-keyed batch save, and built-in logging and OpenTelemetry tracing via IFileService.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.2 52 5/15/2026
3.0.1 170 2/11/2026
3.0.0 450 11/18/2025
2.1.1 302 9/29/2025
2.1.0 247 9/23/2025
2.0.0 364 6/9/2025
1.0.0 319 4/17/2025