Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedded resource in .Net Core libraries

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?

like image 377
Don Tomato Avatar asked Aug 04 '16 08:08

Don Tomato


People also ask

What is embedded resource?

Embedded resource has no predefined structure: it is just a named blob of bytes. So called “. resx” file is a special kind of embedded resource. It is a string -to-object dictionary that maps a name to an object. The object may be a string , an image, an icon, or a blob of bytes.

How do I add a resource to .NET core?

Open Solution Explorer add files you want to embed. Right click on the files then click on Properties. In Properties window and change Build Action to Embedded Resource.

What is the use of embedded resource C#?

Embedded files and their Advantages Embedded files are called as Embedded Resources and these files can be accessed at runtime using the Assembly class of the System. Reflection namespace. Any file within the project can be made into an embedded file.


1 Answers

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].

like image 97
Nacho Coll Avatar answered Sep 21 '22 14:09

Nacho Coll