About • How To Use • Download • Contributors • Versioning • Credits • Related • License
Pathy is a tiny source-only library that will allow you to build file and directory paths by chaining together strings like "c:"
, "dir1"
, "dir2"
using
ChainablePath.New() / "c:" / "dir1" / "dir2"
.
It was heavily inspired by the best build pipeline framework available in the .NET space, Nuke. Nuke has supported these concepts for many years, but I needed this capability outside build pipelines. Lots of kudos to Matthias Koch for what I see as a brilliant idea.
It doesn't have any dependencies and runs on .NET 4.7, .NET 8, as well as frameworks supporting .NET Standard 2.0 and 2.1.
It makes those chained calls to Path.Combine
a thing from the past and hides the ugliness of dealing with (trailing) slashes.
It ships as a source-only package, which means you can use it in your own libraries and projects, without incurring any dependency pain on your consuming projects.
The core Pathy package does not have any dependencies, and I purposely moved the globbing functionality into a separate package as it depends on Microsoft.Extensions.FileSystemGlobbing
.
My name is Dennis Doomen and I'm a Microsoft MVP and Principal Consultant at Aviva Solutions with 28 years of experience under my belt. As a software architect and/or lead developer, I specialize in designing full-stack enterprise solutions based on .NET as well as providing coaching on all aspects of designing, building, deploying and maintaining software systems. I'm the author of several open-source projects such as Fluent Assertions, Reflectify, Liquid Projections, and I've been maintaining coding guidelines for C# since 2001.
Contact me through Email, Bluesky, Twitter/X or Mastadon
This library is available as a NuGet package on https://nuget.org. To install it, use the following command-line:
dotnet add package Pathy
It all starts with the construction of a ChainablePath
instance to represent a path to a file or directory.
There are several ways of doing that.
var path = ChainablePath.From("c:") / "my-path" / "to" / "a" /"directory");
var path = ChainablePath.New() / "c:" / "my-path" / "to" / "a" / "directory";
var path = "c:/mypath/to/a/directory".ToPath();
var path = (ChainablePath)"c:/mypath/to/a/directory";
Additionally, you can use ChainablePath.Current
to get the current working directory as an instance of ChainablePath
, and ChainablePath.Temp
to get that for the user's temporary folder.
The first thing you'll notice is how the /
operator is used to chain multiple parts of a path together. This is the primary feature of Pathy. And it doesn't matter if you do that on Linux or Windows. Internally it'll use whatever path separator is suitable.
You can also use the +
operator to add some phrase to the path without using a separator.
var path = ChainablePath.From("c:") / "my-path" / "to" / "a" / "directory";
path = path + "2"
// Returns "c:/my-path/to/a/directory2"
string result = path.ToString();
To convert an instance of ChainablePath
back to a string
, you can either call ToString()
or cast the instance to a string
.
string rawPath = path.ToString();
string rawPath = (string)path;
Know that ChainablePath
overrides Equals
and GetHashCode
, so you can always compare two instances as you're used to.
Given an instance of ChainablePath
, you can get a lot of useful information:
Name
returns the full name, but without the directory, whereasExtension
gives you the extension including the dot.Directory
,Parent
orDirectoryName
give you the (parent) directory of a file or directory.- To see if a path is absolute, use
IsRooted
- Not sure if a path points to an actual file system entry? Use
IsFile
,IsDirectory
orExists
- Want to know the delta between two paths? Use
AsRelativeTo
.
And if the built-in functionality really isn't enough, you can always call ToDirectoryInfo
or ToFileInfo
to continue with an instance of DirectoryInfo
and FileInfo
.
If you add the Pathy.Globbing
NuGet source-only package as well, you'll get access to the GlobFiles
method. With that, you can fetch a collection of files like this:
ChainablePath[] files = (ChainablePath.Current / "Artifacts").GlobFiles("**/*.json");
Next to that, Pathy also provides a bunch of extension methods to operate on the file-system:
CreateDirectoryRecursively
DeleteFileOrDirectory
MoveFileOrDirectory
To build this repository locally so you can contribute to it, you need the following:
- The .NET SDKs for .NET 4.7, 8.0.
- Visual Studio, JetBrains Rider or Visual Studio Code with the C# DevKit
You can also build, run the unit tests and package the code using the following command-line:
build.ps1
Or, if you have, the Nuke tool installed:
nuke
Also try using --help
to see all the available options or --plan
to see what the scripts does.
Your contributions are always welcome! Please have a look at the contribution guidelines first.
Previous contributors include:
(Made with contrib.rocks)
This library uses Semantic Versioning to give meaning to the version numbers. For the versions available, see the tags on this repository.
This library wouldn't have been possible without the following tools, packages and companies:
- Nuke - Smart automation for DevOps teams and CI/CD pipelines by Matthias Koch
- xUnit - Community-focused unit testing tool for .NET by Brad Wilson
- Coverlet - Cross platform code coverage for .NET by Toni Solarin-Sodara
- GitVersion - From git log to SemVer in no time
- ReportGenerator - Converts coverage reports by Daniel Palme
- StyleCopyAnalyzer - StyleCop rules for .NET
- Roslynator - A set of code analysis tools for C# by Josef Pihrt
- CSharpCodingGuidelines - Roslyn analyzers by Bart Koelman to go with the C# Coding Guidelines
- Meziantou - Another set of awesome Roslyn analyzers by Gérald Barré
- Verify - Snapshot testing by Simon Cropp
- My Blog
- PackageGuard - Get a grip on your open-source packages
- Reflectify - Reflection extensions without causing dependency pains
- .NET Library Package Templates - "dotnet new" templates for building NuGet-published multi-targeting libraries with all the bells and whistles
- FluentAssertions - Extension methods to fluently assert the outcome of .NET tests
- C# Coding Guidelines - Forkable coding guidelines for all C# versions
This project is licensed under the MIT License - see the LICENSE file for details.