ToolBX.FileGuy 2.2.0

dotnet add package ToolBX.FileGuy --version 2.2.0
NuGet\Install-Package ToolBX.FileGuy -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="ToolBX.FileGuy" Version="2.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ToolBX.FileGuy --version 2.2.0
#r "nuget: ToolBX.FileGuy, 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.
// Install ToolBX.FileGuy as a Cake Addin
#addin nuget:?package=ToolBX.FileGuy&version=2.2.0

// Install ToolBX.FileGuy as a Cake Tool
#tool nuget:?package=ToolBX.FileGuy&version=2.2.0

FileGuy

FileGuy

High-level API for handling files.

Setup

With [AutoInject]

If it's not already done, you can add necessary services using in your initialization code :

services.AddAutoInjectServices();
services.AddAutoConfig();

Without [AutoInject]

Use the following extension method in your initialization code :

services.AddFileGuy();

FileFetcher

Returns the URIs of files in a directory and/or its subdirectories.

//This will fetch all files from c:\temp\ regardless of their extension and will not look in subdirectories
var files = _fileFetcher.Fetch("c:\temp\");

//This will only return doc, docx, txt and pdf files from the root of c:\temp\
var files = _fileFetcher.Fetch("c:\temp\", "doc", "docx", "txt", "pdf")

var files _fileFetcher.Fetch("c:\temp\", new FileFetchOptions 
{
	//If omitted, will return all files regardless of extension
	FileExtensions = new List<string> { "pdf", "png" },
	//Will look in all subdirectories as well (default behavior : TopDirectoryOnly)
	SearchKind = SearchOption.AllDirectories,
	//Will return only a relative path to the file (default : Absolute)
	UriKind = UriKind.Relative
});

Default options

There is a way to override default options for your entire project. You need to add the following section to your appsettings.json file. If you don't have an appsettings.json file, then you need to add one to your project.

In the case of console applications, you will have to manually load an appsettings.json file or you can use the AssemblyInitializer.Console package which does this for you.

{
	"DefaultFileFetching": {
		"SearchKind": "AllDirectories",
		"UriKind": "Relative"
	}
}

This will ensure that everytime you use FileFetcher while omitting options, it will default to AllDirectories searches and Relative URIs.

FileLoader

Loads files into memory with or without compression.

LoadAsString

Reads an uncompressed file's content and outputs it as a string.

var file = _fileLoader.LoasAsString("c:/temp/file.txt");

LoadAsBytes

Reads an uncompressed file's content and outputs it as a byte array.

var file = _fileLoader.LoadAsBytes("c:/temp/file.txt");

LoadAsStream

Loads an uncompressed file in memory as a stream.

using var file = _fileLoader.LoadAsStream("c:/temp/file.txt");

DecompressAsString

Loads and decompresses a file's content as string.

using var file = _fileLoader.DecompressAsString("c:/temp/file.zip");

DecompressAsBytes

Loads and decompresses a file's content as a byte array.

using var file = _fileLoader.DecompressAsBytes("c:/temp/file.zip");

DecompressAsStream

Loads and decompresses a file in memory as a stream.

using var file = _fileLoader.DecompressAsStream("c:/temp/file.zip");

FileSaver

Saves a file with or without compression while also handling common use cases.

FileSaveOptions.DuplicateNameBehavior

Dictates what is to be done when a file with the same name exists.

  • Overwrite (Default) : Existing file will be overwritten by the new one
  • Keep : Existing file will be kept and the new file will have the same name followed by "(x)" (where x is an auto-incremented number)
  • Throw : Will throw an exception

FileSaveOptions.CompressionLevel

Default : No compression

See the official .NET documentation for the CompressionLevel enum.

Save methods

Comes in three flavors :

  • void Save(string text, string path, FileSaveOptions? options = null)
  • void Save(byte[] file, string path, FileSaveOptions? options = null)
  • void Save(IStream stream, string path, FileSaveOptions? options = null)

StreamCompressor

Compresses and decompresses streams using GZIP.

Compress

using var compressedStream = _streamCompressor.Compress(stream);

Decompress

using var decompressedStream = _streamCompressor.Decompress(stream);

UniqueFileNameGenerator

Generate

Generates a unique file name based on existing files within a directory to avoid file name collisions.

//Will return "somephoto.png" or "somephoto (1).png" or "somephoto (2).png" etc... if the file already exists
var fileName = _uniqueFileNameGenerator.Generate("c:/somepath/somephoto.png");
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 (2)

Showing the top 2 NuGet packages that depend on ToolBX.FileGuy:

Package Downloads
ToolBX.FileGuy.Newtonsoft

Adds a FileSerializer service which takes advantage of FileGuy's features to serialize objects to file using Newtonsoft's Json.NET library.

ToolBX.FileGuy.Json

Adds a FileSerializer service which takes advantage of FileGuy's features to serialize objects to file using Microsoft's System.Text json library.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.2.0 156 1/13/2024
2.2.0-beta2 79 1/11/2024
2.2.0-beta1 103 1/7/2024
2.0.2 201 6/19/2023
2.0.1 236 4/27/2023
2.0.0 376 11/12/2022
2.0.0-beta1 140 10/7/2022
1.0.3 594 10/7/2022
1.0.1 389 5/19/2022
1.0.0 592 5/16/2022