Cocoar.FileSystem 2.2.0

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

Cocoar.FileSystem

NuGet License .NET

Production-ready file system utilities for .NET with automatic error recovery and multi-platform support.

๐Ÿš€ Features

  • ๐Ÿ›ก๏ธ Resilient File System Monitoring - Production-ready FileSystemWatcher with automatic fallback
  • ๐Ÿ”„ Auto-Recovery - Handles directory disappearing/reappearing, permission errors, watcher crashes
  • โšก Efficient & Reliable - Automatic switching between FileSystemWatcher (efficient) and polling (resilient)
  • ๐ŸŽฏ Debouncing - Built-in debouncing to reduce noise from rapid file changes
  • ๐Ÿš€ High-Performance File Search - Fast directory traversal with lazy evaluation
  • ๐Ÿ” Secure File Reading - Read files as bytes with shared access support (prevent immutable strings in memory)
  • ๐ŸŒ Cross-Platform - Tested on Windows, Linux, and macOS
  • ๐Ÿ“ฆ Zero Dependencies - Lightweight with no external dependencies

๐Ÿ“ฅ Installation

dotnet add package Cocoar.FileSystem

๐Ÿ“– Quick Start

Resilient File System Monitoring

Monitor directories with automatic error recovery and fallback:

using Cocoar.FileSystem;

var monitor = ResilientFileSystemMonitor
    .Watch(@"C:\configs", "*.json")
    .WithDebounce(500) // Optional: reduce noise from rapid changes
    .OnChanged((sender, e) => Console.WriteLine($"Changed: {e.Name}"))
    .OnCreated((sender, e) => Console.WriteLine($"Created: {e.Name}"))
    .Build();

// Multiple file patterns (certificates example):
var monitor = ResilientFileSystemMonitor
    .Watch(@"C:\certs")
    .WithFilter("*.pfx", "*.p12", "*.cer") // Monitor multiple extensions
    .OnChanged((sender, e) => Console.WriteLine($"Certificate changed: {e.Name}"))
    .Build();

// With subdirectory monitoring (depth control):
var monitor = ResilientFileSystemMonitor
    .Watch(@"C:\configs", "*.json")
    .IncludeSubdirectories(2) // Monitor up to 2 levels deep
    .OnChanged((sender, e) => Console.WriteLine($"Changed: {e.FullPath}"))
    .Build();

// Detect folder renames (certificate rotation scenario):
var monitor = ResilientFileSystemMonitor
    .Watch(@"C:\certs")
    .WithFilter("*.pfx")
    .IncludeSubdirectories()
    .OnRenamed((sender, e) => 
    {
        // Fires when folders containing .pfx files are renamed
        // OR when individual .pfx files are renamed
        Console.WriteLine($"Renamed: {e.OldFullPath} -> {e.FullPath}");
    })
    .Build();

Key Features: Auto-recovery, debouncing, depth control, reactive streams, health checks
๐Ÿ“– Full Guide | Reactive Examples


Fast, lazy-evaluated directory traversal with fluent API:

using Cocoar.FileSystem;

// Find all C# files in a project (excluding build folders)
var codeFiles = FileSearcher
    .Search(@"C:\repos\myproject", "*.cs")
    .Excluding("bin", "obj", "node_modules")
    .WithMaxDepth(5)
    .ToList();

// Multiple file patterns (harmonized with ResilientFileSystemMonitor API)
var projectFiles = FileSearcher
    .InDirectory(@"C:\repos\myapp")
    .WithFilter("*.cs", "*.csproj", "*.json")
    .IncludeSubdirectories(2)  // Search 2 levels deep
    .ToList();

// Lazy evaluation with LINQ
var largeFiles = FileSearcher
    .InDirectory(@"C:\data")
    .WithPattern("*.log")
    .Recursively()
    .Where(file => new FileInfo(file).Length > 1_000_000)
    .Take(10);

// Search only current directory (no recursion)
var configs = FileSearcher
    .Search(@"C:\app", "*.json")
    .WithMaxDepth(0)  // Current directory only
    .ToArray();

Key Features: Lazy evaluation, depth limits, exclusion patterns, LINQ support, efficient for large directories
๐Ÿ“– Examples


Secure File Reading

Read files with shared access and security features:

using Cocoar.FileSystem;

byte[] content = FileReader.ReadAllBytes(@"C:\config.dat");
try 
{
    ProcessSensitiveData(content);
}
finally
{
    Array.Clear(content, 0, content.Length); // Zero out when done
}

Key Features: Shared read/write access, BOM stripping, try-pattern, byte array zeroing
๐Ÿ“– Full Guide


๐Ÿ“š Documentation

๐Ÿ”‘ Key Benefits Over Raw FileSystemWatcher

  • โœ… No crashes when directory doesn't exist initially
  • โœ… No silent failures when watcher encounters errors
  • โœ… Handles Docker volume mounts appearing after container start
  • โœ… Handles network share availability issues
  • โœ… Built-in debouncing (no need for manual throttling)
  • โœ… Production-ready error handling
  • โœ… Cross-platform compatibility

๐Ÿ› ๏ธ Configuration Options

Option Type Default Description
Path string (required) Directory path to monitor
Filter string "*" File filter pattern (e.g., ".json", ".txt") - supports multiple patterns
IncludeSubdirectories bool false Monitor subdirectories recursively
MaxDepth int 0 Maximum subdirectory depth (0=root only, -1=unlimited)
NotifyFilter NotifyFilters LastWrite \| FileName \| Size Types of changes to watch for
EnablePollingFallback bool true Enable automatic polling fallback
PollingInterval TimeSpan 5 seconds How often to poll when in fallback mode
AutoRecoverFromErrors bool true Automatically switch to polling on errors
DebounceTime TimeSpan? null Optional debouncing to reduce event noise

Subdirectory Depth Control

Control how deep to monitor subdirectories:

// Don't monitor subdirectories (default)
.Watch(@"C:\certs")  // Only watches C:\certs\*.pfx

// Monitor all subdirectories (unlimited depth)
.Watch(@"C:\certs")
.IncludeSubdirectories()  // or .IncludeSubdirectories(true) or .IncludeSubdirectories(-1)

// Monitor with depth limit
.Watch(@"C:\certs")
.IncludeSubdirectories(1)  // Only direct children: C:\certs\prod\*.pfx
.IncludeSubdirectories(2)  // Two levels: C:\certs\prod\2025\*.pfx

๐Ÿงต Thread Safety

All operations are thread-safe. Events are raised sequentially on a background thread, guaranteeing:

  • โœ… Events are delivered in the order they occurred
  • โœ… No concurrent event handler invocations (unless you subscribe multiple handlers)
  • โœ… When using the Events channel, you have full control over concurrency

๐Ÿ—‘๏ธ Disposal

Always dispose when done:

using var monitor = ResilientFileSystemMonitor
    .Watch(@"C:\data")
    .Build();
// Use monitor...
// Automatically disposed at end of scope

๐Ÿค Contributing & Versioning

  • SemVer (additive MINOR, breaking MAJOR)
  • PRs & issues welcome

๐Ÿ“„ License & Trademark

This project is licensed under the Apache License, Version 2.0. See NOTICE for attribution.

"Cocoar" and related marks are trademarks of COCOAR e.U. Use of the name in forks or derivatives should preserve attribution and avoid implying official endorsement. See TRADEMARKS for permitted and restricted uses.

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.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Cocoar.FileSystem:

Package Downloads
Cocoar.Configuration

Reactive, strongly-typed configuration layering for .NET with health monitoring, file resilience, and zero-downtime updates. Core library providing ConfigManager, providers (file, environment, command-line, observable), and differential updates.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.2.0 186 11/15/2025
2.1.0 529 11/13/2025
2.0.0 270 11/12/2025
1.0.0 197 11/5/2025
0.1.0-alpha.15 138 11/4/2025
0.1.0-alpha.14 131 11/4/2025
0.1.0-alpha.13 131 11/4/2025
0.1.0-alpha.12 135 11/4/2025