104

I just have started looking into .Net Core, and I don't see classical resources and anything what looks like resources. In classical .Net class libraries I was able to add, for example, text filtes with some script to my project, than I can add these files to project's resources. After that I could easily use that by the following way:

Connection.Execure(Properties.Resources.MySuperScript);

I see that there isn't such feature in .Net Core libraries, at least I don't see. Is there an alternative in .Net Core to store some statical data as an embedded resource in libraries? And how to use that if it exists?

4

6 Answers 6

131

UPDATE:

.NET Core 1.1 and later have dropped project.json and returned to .csproj files. This changes Step 2, but not all that much. The necessary lines are very similar:

<ItemGroup>
  <Content Remove="_fonts/OpenSans.ttf" />
  <Content Remove="_fonts/OpenSans-Bold.ttf" />
  <Content Remove="_fonts/OpenSans-Italic.ttf" />
</ItemGroup>
<ItemGroup>
  <EmbeddedResource Include="_fonts/OpenSans.ttf" />
  <EmbeddedResource Include="_fonts/OpenSans-Bold.ttf" />
  <EmbeddedResource Include="_fonts/OpenSans-Italic.ttf" />
</ItemGroup>

There may be a similar *.tff form; unconfirmed.

Steps 1 and 3 are unchanged.


To use embedded resources in .NET Core 1.0 project do the following:

  1. Add your embedded file(s) as usual.

    Example: some FONT files on a directory named "_fonts"

    enter image description here

  2. Modify "project.json" to include the related resources.

    In my case:

     "buildOptions": {
        "embed": {
          "include": [
            "_fonts/*.ttf"    
          ]
        } 
      },
    
  3. Access the embedded resource in code.

    var assembly = typeof(MyLibrary.MyClass).GetTypeInfo().Assembly;
    Stream resource = assembly.GetManifestResourceStream("MyLibrary._fonts.OpenSans.ttf");
    

    The key point is to use the right name on GetManifestResourceStream call. You have to use [assembly name].[directory].[file name].

8
  • 45
    to be sure you are fetching the right resource, evaluate all the resources available in your target assembly string[] names = assembly.GetManifestResourceNames();
    – Luka
    Commented Mar 1, 2017 at 9:37
  • 1
    This works for me, but it still ends up using a file path which is in the root of the executed assembly. I have two dlls, one for API and one for CSV. API is the library which is run. CSV contains the files I need to read. Even if the files are listed out as "Mylibrary.folder.file", which is "CSV.csv.somefile.csv" it will attempt to read "solution/API/somefile.csv". The path I would expect it to read would be "solution/CSV/csv/somefile.csv". Any suggestions for how to preserve the relative path within the assembly?
    – melwil
    Commented Mar 8, 2017 at 11:13
  • Thank you @Luka your comment helped me identify the actual names of the embedded resources! Commented May 25, 2017 at 17:08
  • what @Zartag says make sense except for the fact that files are not reachable by using path, the main solution works yes, but there are some use cases where make no sense read the stream just like that Commented Aug 14, 2017 at 19:12
  • 3
    Looks like the format of the resource name uses the default namespace of the assembly, instead of the assembly name. Still dot-delimited as stated above, though.
    – tiwahu
    Commented Jan 26, 2018 at 21:09
61

Now that project.json is deprecated, you have to specify this in the .csproj file.

<ItemGroup>
    <EmbeddedResource Include="_fonts\*.ttf" />
</ItemGroup>

You can use a wildcard as shown, or just list out the files explicitly.

5
  • 12
    This isn't much use without the details of how to get the data out afterwards.
    – Brondahl
    Commented Oct 16, 2018 at 15:59
  • Where are the resources located on disk after compilation? I'd expect to find *.ttf somewhere inside the /bin folder.
    – Jim Aho
    Commented Dec 14, 2018 at 12:37
  • 6
    @JimAho the files are embedded in the assembly. Commented Dec 14, 2018 at 17:31
  • 1
    @Brondahl the question is about embedding resources, and this is how to do it. :) The OP probably knows where to go from here. For others looking for how to do it, here's an example implementation related to testing with xunit: patriksvensson.se/2017/11/… Commented May 7, 2019 at 8:23
  • 1
    The link from Johny no longer works, here is the archived version: web.archive.org/web/20201024173213/https://patriksvensson.se/…
    – majjam
    Commented Mar 8, 2021 at 16:25
44

With newer versions of .Net Core - 2.0 or greater - there's a specialized class EmbeddedFileProvider that abstract the embedded file reading. To use it, add Microsoft.Extensions.FileProviders.Embedded package to your application:

dotnet add package Microsoft.Extensions.FileProviders.Embedded

The EmbeddedFileProvider allows you to create a stream reader, and use according to your scenario:

var embeddedProvider = new EmbeddedFileProvider(Assembly.GetExecutingAssembly());
using (var reader = embeddedProvider.GetFileInfo("yourfile.ext").CreateReadStream())
{
  // some logic with stream reader
}
5
  • 1
    This seemed promising, but I had issues with files in subfolders. I ended up going with my own matching logic off using string[] names = assembly.GetManifestResourceNames(); that Luka mentioned above. Commented Nov 20, 2020 at 3:58
  • 2
    The file format for GetFileInfo() is "{Folder}.{filename}.{Extension}" WITHOUT the namespace. This is different to assembly.GetManifestResourceStream() which uses the file format "{Namespace}.{Folder}.{filename}.{Extension}" including the Namespace Commented Feb 5, 2021 at 22:07
  • I am getting a can't find resource error. I added the file to Images then changed the file type to embedded. var resourceName = "X_Signature.jpg"; using (var stream = assembly.GetFileInfo(resourceName).CreateReadStream()) { Commented Mar 7, 2022 at 16:05
  • I set the resource file as an embedded resource and set a custom namespace.resources Commented Mar 7, 2022 at 16:48
  • @JimbobTheSailor Your comment was so key to getting this to work. MS should add this to the method info page
    – KornMuffin
    Commented Mar 14, 2023 at 3:00
23

People have already generally answered this, so this is a rendering of the answers into something simple.

Before using the following, the file should be added as an embedded resource to the .csproj / project.json

Usage

var myJsonFile = ReadManifestData<Tests>("myJsonFile.json");
  1. Parameter: embedded filename name; Type: any class from the target resource's assembly
  2. looks for an embedded resource with that name
  3. returns the string value

Method

public static string ReadManifestData<TSource>(string embeddedFileName) where TSource : class
{
    var assembly = typeof(TSource).GetTypeInfo().Assembly;
    var resourceName = assembly.GetManifestResourceNames().First(s => s.EndsWith(embeddedFileName,StringComparison.CurrentCultureIgnoreCase));

    using (var stream = assembly.GetManifestResourceStream(resourceName))
    {
        if (stream == null)
        {
            throw new InvalidOperationException("Could not load manifest resource stream.");
        }
        using (var reader = new StreamReader(stream))
        {
            return reader.ReadToEnd();
        }
    }
}
1
  • This is the only one that worked for me. Thanks much!
    – IamIC
    Commented Jun 16, 2021 at 16:27
5

In dotnet 7, I needed to keep a text file as resource in a Razor Class Library project. To read that file I had to do the following -

  1. Add the file in csproj file
<ItemGroup>
  <EmbeddedResource Include="Resources/**/*" />
</ItemGroup>

Here, Resources is the folder I created in the Razor Class Library Project.

  1. Read the content of the file
private string ReadHtmlString()
{
    var embeddedProvider = new EmbeddedFileProvider(typeof(PageModelClass).Assembly);
    using var reader = embeddedProvider.GetFileInfo("Resources/filename.txt").CreateReadStream();
    using var sr = new StreamReader(reader);

    var content = sr.ReadToEnd();
    return content;
}

Here, PageModelClass is the Pages Model Class in the Razor Class Library.

4

I have not confirmed this in documentation, but for me, it would appear the auto-generated Resource code that retrieves embedded files found in Resource.Designer.cs is now functioning again in .NET Core 3.1. I can now retrieve an embedded jpg simply by calling the Properties.Resources.MyImageName which returns a Bitmap object.

2
  • streamreader retrieves characters only. This works if your resource file is character based Commented Jun 22, 2020 at 21:28
  • Yes, this works now, but you need to add System.Resources.Extensions package and add <GenerateResourceUsePreserializedResources>true</GenerateResourceUsePreserializedResources> in cs/vbproj. but it's could cause some problems when used with .net framework projects. See here
    – fsbflavio
    Commented Oct 16, 2021 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.