EinsTools.Utilities.FileSystem 0.0.1.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package EinsTools.Utilities.FileSystem --version 0.0.1.3
NuGet\Install-Package EinsTools.Utilities.FileSystem -Version 0.0.1.3
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="EinsTools.Utilities.FileSystem" Version="0.0.1.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EinsTools.Utilities.FileSystem --version 0.0.1.3
#r "nuget: EinsTools.Utilities.FileSystem, 0.0.1.3"
#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.
// Install EinsTools.Utilities.FileSystem as a Cake Addin
#addin nuget:?package=EinsTools.Utilities.FileSystem&version=0.0.1.3

// Install EinsTools.Utilities.FileSystem as a Cake Tool
#tool nuget:?package=EinsTools.Utilities.FileSystem&version=0.0.1.3

EinsTools.Utilities.FileSystem

A small library with extensions to the DirectoryInfo and FileInfo classes.

DirectoryInfo Extensions

GetDirectory

public static DirectoryInfo GetDirectory(this DirectoryInfo directory, string path)

Returns a DirectoryInfo object for the specified path relative to the current directory. The path can be absolute or relative. If the path is relative, it is combined with the current directory to form an absolute path.

The function does not check if the directory exists.

var directory = new DirectoryInfo(@"C:\Temp");
var subDirectory = directory.GetDirectory("SubDirectory");

GetFile

public static FileInfo GetFile(this DirectoryInfo directory, string path)

Returns a FileInfo object for the specified path relative to the current directory. The path can be absolute or relative. If the path is relative, it is combined with the current directory to form an absolute path.

The function does not check if the file exists.

var directory = new DirectoryInfo(@"C:\Temp");
var file = directory.GetFile("SubDirectory\file.txt");

FullNameWithEnding

public static string FullNameWithEnding(this DirectoryInfo directory)

Returns the full name of the directory with a trailing backslash or slash depending on the operating system.

Under Linux or MacOS, the function returns the full name with a trailing slash (e.g. "/home/user/"). On Windows, the function returns the full name with a trailing backslash (e.g. "C:\Users\user"). Additionally, the function will replace a trailing slash with a backslash on Windows (e.g. "C:\Users\user/" ⇒ "C:\Users\user").

var directory = new DirectoryInfo(@"C:\Temp");
var fullName = directory.FullNameWithEnding(); // "C:\Temp\" on Windows

FullNameWithoutEnding

public static string FullNameWithoutEnding(this DirectoryInfo directory)

Returns the full name of the directory without a trailing backslash or slash depending on the operating system.

var directory = new DirectoryInfo(@"C:\Temp\");
var fullName = directory.FullNameWithoutEnding(); // "C:\Temp" on Windows

DeleteFiles

public static void DeleteFiles(this DirectoryInfo @this, string searchPattern = "*",
SearchOption searchOption = SearchOption.TopDirectoryOnly)

Deletes all files in the directory that match the specified search pattern.

var directory = new DirectoryInfo(@"C:\Temp");
directory.DeleteFiles("*.txt", SearchOption.AllDirectories);

DeleteFilesAsync

public static async Task DeleteFilesAsync(this DirectoryInfo @this, string searchPattern = "*",
SearchOption searchOption = SearchOption.TopDirectoryOnly)

Deletes all files in the directory that match the specified search pattern. Just an asynchronous version of DeleteFiles.

var directory = new DirectoryInfo(@"C:\Temp");
await directory.DeleteFilesAsync("*.txt", SearchOption.AllDirectories);

Globbing

public static IEnumerable<string> GlobRelative(this DirectoryInfo @this, params string[] pattern);
public static IEnumerable<string> GlobAbsolute(this DirectoryInfo @this, params string[] pattern);
public static IEnumerable<FileInfo> Glob(this DirectoryInfo @this, params string[] pattern)
public static Task<IEnumerable<string>> GlobRelativeAsync(this DirectoryInfo @this, params string[] pattern);
public static Task<IEnumerable<string>> GlobAbsoluteAsync(this DirectoryInfo @this, params string[] pattern);
public static Task<IEnumerable<FileInfo>> GlobAsync(this DirectoryInfo @this, params string[] pattern)

Globbing is a pattern matching technique similar to regular expressions, but simpler. The pattern is a string that may contain wildcards. You can find the supported wildcards at https://learn.microsoft.com/en-us/dotnet/core/extensions/file-globbing.

GlobRelative returns the paths of the files relative to the given directoy, that match the pattern. GlobAbsolute returns the absolute paths. Glob returns the FileInfo objects of the files that match the pattern and otherwise work like GlobAbsolute. The async versions return a Task<IEnumerable<string>> and work in the same way.

var directory = new DirectoryInfo(@"/Usr/Temp");
var files = directory.GlobRelative("**/*.txt");

EnsureDirectory

public static DirectoryInfo EnsureDirectory(this DirectoryInfo directory, string path)

Like GetDirectory, but creates the directory if it does not exist.

var directory = new DirectoryInfo(@"C:\Temp");
var subDirectory = directory.EnsureDirectory("SubDirectory");

EnsureEmptyDirectory

public static DirectoryInfo EnsureEmptyDirectory(this DirectoryInfo @this, string? subPath = null)

Like EnsureDirectory, but deletes the directory if it exists and creates a new empty directory.

var directory = new DirectoryInfo(@"C:\Temp");
var subDirectory = directory.EnsureEmptyDirectory("SubDirectory");

CopyToAsync

public static async Task<DirectoryInfo> CopyToAsync(this DirectoryInfo @this, DirectoryInfo target, bool overwrite = true)
public static async Task<DirectoryInfo> CopyToAsync(this DirectoryInfo @this, string target, bool overwrite = true)
public static async Task<DirectoryInfo> CopyToAsync(this DirectoryInfo @this, DirectoryInfo target, string subPath, bool overwrite = true)

Copies the directory to the specified target directory. If the target directory does not exist, it is created. If overwrite is true, the files that already exist in the target directory are overwritten. Otherwise, they are skipped.

The second overload takes a string as target path.

The third overload copies the directory to a subdirectory of the target directory.

var directory = new DirectoryInfo(@"C:\Temp");
var target = new DirectoryInfo(@"C:\Target");
var subDirectory = await directory.CopyToAsync(target, true);

CopyToEmptyAsync

public static async Task<DirectoryInfo> CopyToEmptyAsync(this DirectoryInfo @this, DirectoryInfo target, bool overwrite = true) {
public static async Task<DirectoryInfo> CopyToEmptyAsync(this DirectoryInfo @this, string target, bool overwrite = true) {
public static async Task<DirectoryInfo> CopyToEmptyAsync(this DirectoryInfo @this, DirectoryInfo target, string subPath, bool overwrite = true) {

Like CopyToAsync, but deletes the target directory if it exists and creates a new empty directory.

var directory = new DirectoryInfo(@"C:\Temp");
var target = new DirectoryInfo(@"C:\Target");
var subDirectory = await directory.CopyToEmptyAsync(target, true);

DeserializeAsync

public static async Task<T?> DeserializeAsync<T>(this DirectoryInfo @this, string fileName, JsonSerializerOptions? options = null)

Deserializes the json content of the file to an object of type T.

var directory = new DirectoryInfo(@"C:\Temp");
var data = await directory.DeserializeAsync<MyData>("file.json");

FileInfo Extensions

CopyToAsync

public static async Task<FileInfo> CopyToAsync(this FileInfo @this, FileInfo target, bool overwrite = true) {
public static async Task<FileInfo> CopyToAsync(this FileInfo @this, string target, bool overwrite = true) {

Simple overloads of the CopyTo method of the FileInfo class that run asynchronously.

var file = new FileInfo(@"C:\Temp\file.txt");
var target = new FileInfo(@"C:\Target\file.txt");
var copiedFile = await file.CopyToAsync(target, true);

DeserializeAsync

public static async Task<T> DeserializeAsync<T>(this FileInfo @this, JsonSerializerOptions? options = null)

Deserializes the json content of the file to an object of type T.

var file = new FileInfo(@"C:\Temp\file.json");
var data = await file.DeserializeAsync<MyData>();

SerializeAsync

public static async Task SerializeAsync<T>(this FileInfo @this, T data, JsonSerializerOptions? options = null)

Serializes the object to json and writes it to the file.

var file = new FileInfo(@"C:\Temp\file.json");
var data = new MyData();
await file.SerializeAsync(data);
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
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.1-rc.1 82 9/4/2023
1.0.0 160 8/30/2023
1.0.0-rc.1 77 8/30/2023
1.0.0-rc.0 81 8/29/2023
0.0.3-rc.6 84 8/23/2023
0.0.3-rc.4 69 8/23/2023
0.0.3-rc.3 66 8/23/2023
0.0.3-rc.2 67 8/23/2023
0.0.3-rc.1 55 8/22/2023
0.0.3-rc.0 61 8/22/2023
0.0.2 137 8/22/2023
0.0.2-rc.2 67 8/22/2023
0.0.2-rc.1 68 8/22/2023
0.0.2-rc.0 70 8/21/2023
0.0.1.8 138 8/21/2023
0.0.1.3 143 8/21/2023
0.0.1-rc.0 70 8/21/2023