FluentPath 2.0.0

A fluent wrapper around System.IO

Install-Package FluentPath -Version 2.0.0
dotnet add package FluentPath --version 2.0.0
<PackageReference Include="FluentPath" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FluentPath --version 2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

FluentPath

FluentPath implements a modern wrapper around System.IO, using modern patterns
such as fluent APIs and Lambdas.
By using FluentPath instead of System.IO, you'll be able to chain calls and
act on sets of files as if they were individual files.

Getting started

In order to run the tests for this package from Visual Studio, you'll need to have
SpecFlow and xUnit installed.

Using FluentPath

This was originally published here.

.NET is now more than eight years old, and some of its APIs got old with more
grace than others.
System.IO in particular has always been a little awkward.
It’s mostly static method calls (Path.*, Directory.*, etc.) and some stateful
classes (DirectoryInfo, FileInfo).
In these APIs, paths are plain strings.

Since .NET v1, lots of good things happened to C#: lambda expressions,
extension methods, optional parameters to name just a few.
Outside of .NET, other interesting things happened as well.
For example, you might have heard about this JavaScript library that had some
success introducing a fluent API to handle the hierarchical structure of the
HTML DOM.
You know? jQuery.

Knowing all that, every time I need to use the stuff in System.IO, I cringe.
So I thought I’d just build a more modern wrapper around it.
I used a fluent API based on an essentially immutable Path type and an
enumeration of such path objects.
To achieve the fluent style, a healthy dose of lambda expressions is being used
to act on the objects.

Without further ado, here’s an example of what you can do with the new API.
In that example, I’m using a Media Center extension that wants all video files
to be in their own folder.
For that, I need a small tool that creates directories for each video file and
moves the files in there. Here’s the code for it:

Path.Get(args.Length != 0 ? args[0] : ".")
    .Files(
        p => new[] {
            ".avi", ".m4v", ".wmv",
            ".mp4", ".dvr-ms", ".mpg", ".mkv"
        }.Contains(p.Extension))
    .CreateDirectories(
        p => p.Parent()
              .Combine(p.FileNameWithoutExtension))
    .End()
    .Move(
        p => p.Parent()
              .Combine(p.FileNameWithoutExtension)
              .Combine(p.FileName));

This code creates a Path object pointing at the path pointed to by the first
command line argument of my executable.
It then selects all video files.
After that, it creates directories that have the same names as each of the
files, but without their extension.
The result of that operation is the set of created directories.
We can now get back to the previous set using the Previous method, and finally
we can move each of the files in the set to the corresponding freshly created
directory, whose name is the combination of the parent directory and the
filename without extension.

Moving files around with FluentPath

The new fluent path library covers a fair part of what’s in System.IO in a
single, convenient API.
Check it out, I hope you’ll enjoy it.
Suggestions and contributions are more than welcome.

FluentPath

FluentPath implements a modern wrapper around System.IO, using modern patterns
such as fluent APIs and Lambdas.
By using FluentPath instead of System.IO, you'll be able to chain calls and
act on sets of files as if they were individual files.

Getting started

In order to run the tests for this package from Visual Studio, you'll need to have
SpecFlow and xUnit installed.

Using FluentPath

This was originally published here.

.NET is now more than eight years old, and some of its APIs got old with more
grace than others.
System.IO in particular has always been a little awkward.
It’s mostly static method calls (Path.*, Directory.*, etc.) and some stateful
classes (DirectoryInfo, FileInfo).
In these APIs, paths are plain strings.

Since .NET v1, lots of good things happened to C#: lambda expressions,
extension methods, optional parameters to name just a few.
Outside of .NET, other interesting things happened as well.
For example, you might have heard about this JavaScript library that had some
success introducing a fluent API to handle the hierarchical structure of the
HTML DOM.
You know? jQuery.

Knowing all that, every time I need to use the stuff in System.IO, I cringe.
So I thought I’d just build a more modern wrapper around it.
I used a fluent API based on an essentially immutable Path type and an
enumeration of such path objects.
To achieve the fluent style, a healthy dose of lambda expressions is being used
to act on the objects.

Without further ado, here’s an example of what you can do with the new API.
In that example, I’m using a Media Center extension that wants all video files
to be in their own folder.
For that, I need a small tool that creates directories for each video file and
moves the files in there. Here’s the code for it:

Path.Get(args.Length != 0 ? args[0] : ".")
    .Files(
        p => new[] {
            ".avi", ".m4v", ".wmv",
            ".mp4", ".dvr-ms", ".mpg", ".mkv"
        }.Contains(p.Extension))
    .CreateDirectories(
        p => p.Parent()
              .Combine(p.FileNameWithoutExtension))
    .End()
    .Move(
        p => p.Parent()
              .Combine(p.FileNameWithoutExtension)
              .Combine(p.FileName));

This code creates a Path object pointing at the path pointed to by the first
command line argument of my executable.
It then selects all video files.
After that, it creates directories that have the same names as each of the
files, but without their extension.
The result of that operation is the set of created directories.
We can now get back to the previous set using the Previous method, and finally
we can move each of the files in the set to the corresponding freshly created
directory, whose name is the combination of the parent directory and the
filename without extension.

Moving files around with FluentPath

The new fluent path library covers a fair part of what’s in System.IO in a
single, convenient API.
Check it out, I hope you’ll enjoy it.
Suggestions and contributions are more than welcome.

  • .NETStandard 2.0

    • No dependencies.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
2.0.0 1,839 11/11/2019
1.2.1 7,394 4/2/2016
1.2.0 368 4/2/2016
1.1.0 7,313 9/12/2014
1.0.0 5,368 1/8/2011