Skip to content

Instantly share code, notes, and snippets.

@dradovic
Created February 21, 2019 10:37
Show Gist options
  • Save dradovic/0548310e623391145cfb0c04bd2db772 to your computer and use it in GitHub Desktop.
Save dradovic/0548310e623391145cfb0c04bd2db772 to your computer and use it in GitHub Desktop.
How to use the ManifestEmbeddedFileProvider with a Satellite Assembly
using System;
using System.IO;
using System.Reflection;
using Microsoft.Extensions.FileProviders;
namespace ReadingEmbeddedResource
{
class Program
{
static void Main(string[] args)
{
var provider = new ManifestEmbeddedFileProvider(Assembly.GetEntryAssembly());
var fileInfo = provider.GetFileInfo("Resource.de-CH.json");
Console.WriteLine($"Exists: {fileInfo.Exists}"); // prints true
string json;
using (var reader = new StreamReader(fileInfo.CreateReadStream()))
{
json = reader.ReadToEnd();
}
Console.WriteLine(json);
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="2.1.1" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resource.de-CH.json" />
</ItemGroup>
</Project>
{
"This is a test": "Das ist ein Test"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment