ToolBX.FileGuy
3.0.0
dotnet add package ToolBX.FileGuy --version 3.0.0
NuGet\Install-Package ToolBX.FileGuy -Version 3.0.0
<PackageReference Include="ToolBX.FileGuy" Version="3.0.0" />
paket add ToolBX.FileGuy --version 3.0.0
#r "nuget: ToolBX.FileGuy, 3.0.0"
// Install ToolBX.FileGuy as a Cake Addin #addin nuget:?package=ToolBX.FileGuy&version=3.0.0 // Install ToolBX.FileGuy as a Cake Tool #tool nuget:?package=ToolBX.FileGuy&version=3.0.0
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 | Versions 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. |
-
net8.0
- ToolBX.AutoConfig (>= 3.0.0)
- ToolBX.AutoInject (>= 3.0.0)
- ToolBX.DescriptiveEnums (>= 3.0.0)
- ToolBX.NetAbstractions (>= 3.0.0)
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 |
---|---|---|
3.0.0 | 128 | 9/27/2024 |
3.0.0-beta1 | 96 | 9/23/2024 |
2.2.0 | 203 | 1/13/2024 |
2.2.0-beta2 | 116 | 1/11/2024 |
2.2.0-beta1 | 141 | 1/7/2024 |
2.0.2 | 243 | 6/19/2023 |
2.0.1 | 282 | 4/27/2023 |
2.0.0 | 421 | 11/12/2022 |
2.0.0-beta1 | 181 | 10/7/2022 |
1.0.3 | 644 | 10/7/2022 |
1.0.1 | 424 | 5/19/2022 |
1.0.0 | 648 | 5/16/2022 |