Skip to content

Instantly share code, notes, and snippets.

@akunzai
Last active November 28, 2022 20:38
Show Gist options
  • Select an option

  • Save akunzai/3574a9c5974583dabb930e5d0729cef7 to your computer and use it in GitHub Desktop.

Select an option

Save akunzai/3574a9c5974583dabb930e5d0729cef7 to your computer and use it in GitHub Desktop.
Microsoft.Extensions.FileProviders Extension Methods
using System;
using System.IO;
using System.Text.Json;
namespace Microsoft.Extensions.FileProviders;
public static class FileProviderExtensions
{
public static Stream ReadAsStream(this IFileProvider fileProvider, string path)
{
return fileProvider.GetFileInfo(path).CreateReadStream();
}
public static string ReadAsString(this IFileProvider fileProvider, string path)
{
using var reader = new StreamReader(fileProvider.ReadAsStream(path));
return reader.ReadToEnd();
}
public static T ReadAsObject<T>(this IFileProvider fileProvider,
string path,
Action<T> configAction = null,
JsonSerializerOptions options = null)
{
using var stream = fileProvider.ReadAsStream(path);
var value = JsonSerializer.Deserialize<T>(stream, options);
configAction?.Invoke(value);
return value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment