Zhomfr.Helpers.SmartFile 0.7.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Zhomfr.Helpers.SmartFile --version 0.7.0
                    
NuGet\Install-Package Zhomfr.Helpers.SmartFile -Version 0.7.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="Zhomfr.Helpers.SmartFile" Version="0.7.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Zhomfr.Helpers.SmartFile" Version="0.7.0" />
                    
Directory.Packages.props
<PackageReference Include="Zhomfr.Helpers.SmartFile" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Zhomfr.Helpers.SmartFile --version 0.7.0
                    
#r "nuget: Zhomfr.Helpers.SmartFile, 0.7.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.
#:package Zhomfr.Helpers.SmartFile@0.7.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Zhomfr.Helpers.SmartFile&version=0.7.0
                    
Install as a Cake Addin
#tool nuget:?package=Zhomfr.Helpers.SmartFile&version=0.7.0
                    
Install as a Cake Tool

Helpers for File Streams

NuGet Status NuGet Downloads

⚠️ Warning: Active Development (< 1.0.0)

Prior to the release of stable version 1.0.0, this library follows SemVer for initial development. This means breaking changes may occur even in minor releases (0.x).

We strongly recommend pinning the package version in your .csproj to avoid unexpected build errors.

Helpers for working with file streams. For detection of mimetype used libmagic.

📦 Installation

Install package:

dotnet add package Zhomfr.Helpers.SmartFile

After add namespace in using section:

using Zhomfr.Helpers.SmartFile;

If you need automatic bind

using Zhomfr.Helpers.SmartFile;

builder.Services.AddControllers(options =>
{
    options.ModelBinderProviders.Add(new SmartFileModelBinderProvider());
});

SmartFile

Namespace: Zhomfr.Helpers.SmartFile

Represents a self-contained file object that manages its own data access. Operates on top of a Stream to provide high-level content manipulation.

public class SmartFile : System.IDisposable, System.IAsyncDisposable

Inheritance Object → SmartFile<br> Implements IDisposable, IAsyncDisposable<br> Attributes NullableContextAttribute, NullableAttribute

Remarks:

Lifecycle Management: When this object is provided via dependency injection or a Model Binder in ASP.NET Core, it is typically registered for automatic disposal at the end of the HTTP request. In such cases, you do not need to call SmartFile.DisposeAsync () manually.

Manual Usage: If you instantiate this class manually outside a managed scope (e.g., in a background task or a console application), you must ensure it is disposed of to release underlying resources and delete any temporary buffering files.

Properties

FileName

Gets the name of the file, including the extension.

public string FileName { get; }
Property Value

String<br>

Extension

Gets the file extension (e.g., ".png").

Remarks:

Known Limitation: .ico files may currently be incorrectly identified as generic binary files (.bin). This issue is planned to be resolved in upcoming releases.

public string Extension { get; }
Property Value

String<br>

ContentType

Gets the MIME type as declared by the source.

public string ContentType { get; }
Property Value

String<br>

Length

Gets the file size in bytes.

public long Length { get; }
Property Value

Int64<br>

Remarks:

Accessing this property on a non-seekable stream will trigger full buffering to determine the length.

Constructors

SmartFile (Stream, String)

Initializes a new instance of the SmartFile class using a data stream.

public SmartFile(Stream stream, string fileName)
Parameters

stream Stream<br> The source stream containing file data.

fileName String<br> The name of the file including extension.

Exceptions

ArgumentNullException<br> Thrown when the stream is null.

SmartFile (Byte[], String)

Initializes a new instance of the SmartFile class using a byte array.

public SmartFile(Byte[] bytes, string fileName)
Parameters

bytes Byte[]<br> The byte array containing file data.

fileName String<br> The name of the file.

SmartFile (String, String)

Initializes a new instance of the SmartFile class using a string content.

public SmartFile(string content, string fileName)
Parameters

content String<br> The string content to be stored as a file (encoded in UTF-8).

fileName String<br> The name of the file.

SmartFile (Object, String, JsonSerializerOptions)

Initializes a new instance of the SmartFile class by serializing an object to JSON.

public SmartFile(object data, string fileName, JsonSerializerOptions? options = null)
Parameters

data Object<br> The object to serialize into the file content.

fileName String<br> The name of the file (e.g., "data.json").

options JsonSerializerOptions?<br> Optional JSON serializer settings.

Methods

DisposeAsync ()

public async ValueTask DisposeAsync()
Returns

ValueTask<br>

Dispose ()

public void Dispose()

GetStream ()

Returns the underlying data stream. Ensures the stream is seekable by transparently buffering it if necessary.

public Stream GetStream()
Returns

Stream<br> A Stream positioned at the beginning (0).

Exceptions

ObjectDisposedException<br> If object already disposed.

AsString ()

Reads the entire file content as a UTF-8 string, resetting the stream position to the start.

public string AsString()
Returns

String<br> The file content as a string.

AsStringAsync (CancellationToken)

Asynchronously reads the entire content of the file as a UTF-8 string, resetting the stream position to the start.

public async Task<string> AsStringAsync(CancellationToken ct = null)
Parameters

ct CancellationToken<br> The cancellation token.

Returns

Task<String><br> A task representing the string content of the file.

AsBytes ()

Reads the entire file content into a byte array, resetting the stream position to the start.

public Byte[] AsBytes()
Returns

Byte[]<br> A byte array containing the file data.

AsBytesAsync (CancellationToken)

Asynchronously reads the entire content of the file into a byte array, resetting the stream position to the start.

public async Task<Byte[]> AsBytesAsync(CancellationToken ct = null)
Parameters

ct CancellationToken<br> The cancellation token.

Returns

Task<Byte[]><br> A task representing the byte array of the file data.

AsJson<T> (JsonSerializerOptions)

Deserializes the file content from JSON into the specified type.

public T AsJson<T>(JsonSerializerOptions? options = null)
Type Parameters

T<br> The type to deserialize to.

Parameters

options JsonSerializerOptions?<br> Optional JSON serializer settings.

Returns

T<br> The deserialized object, or null if deserialization fails.

AsJsonAsync<T> (JsonSerializerOptions, CancellationToken)

Asynchronously deserializes the file content from JSON into the specified type.

public async Task<T> AsJsonAsync<T>(JsonSerializerOptions? options = null, CancellationToken ct = null)
Type Parameters

T<br> The type to deserialize to.

Parameters

options JsonSerializerOptions?<br> Optional JSON serializer settings.

ct CancellationToken<br> The cancellation token.

Returns

Task<T><br> A task representing the deserialized object.

GetRealMimeType ()

Performs a deep binary inspection (magic numbers) to determine the actual MIME type.

public string GetRealMimeType()
Returns

String<br>

Remarks:

This method will trigger stream buffering if the underlying stream is non-seekable.

GetRealMimeTypeAsync (CancellationToken)

Asynchronously performs a deep inspection of the file's binary signature.

public Task<string> GetRealMimeTypeAsync(CancellationToken ct = null)
Parameters

ct CancellationToken<br>

Returns

Task<String><br>

Hash (HashAlgorithmName)

Get hash of file.

public string Hash(HashAlgorithmName algorithm)
Parameters

algorithm HashAlgorithmName<br> The hashing algorithm (e.g., MD5, SHA256).

Returns

String<br> The hexadecimal string representation of the file hash.

HashAsync (HashAlgorithmName, CancellationToken)

Asynchronously get hash of a file.

public async Task<string> HashAsync(HashAlgorithmName algorithm, CancellationToken ct = null)
Parameters

algorithm HashAlgorithmName<br> The hashing algorithm.

ct CancellationToken<br> The cancellation token.

Returns

Task<String><br> A task representing the hexadecimal file hash.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Zhomfr.Helpers.SmartFile:

Package Downloads
Zhomfr.Validation.Rules.Files

A powerful, intuitive collection of custom validation attributes built directly on top of the standard .NET System.ComponentModel.DataAnnotations infrastructure.

Zhomfr.Storage.Abstractions

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.7.1 128 11/7/2026
0.7.0 118 10/26/2026
0.6.0 133 10/24/2026
0.5.0 119 10/23/2026
0.4.1 100 10/23/2026
0.4.0 109 10/22/2026