Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resources at class library project

I want to add some xml file as resource to my class library project . Any idea how to do so , and call it later? In windows application i would do it like ClassLibrary1.Properties.Resources.file.xml

But here it didn't worked any idea how i do it here ?

like image 212
Night Walker Avatar asked Jun 13 '10 14:06

Night Walker


People also ask

How do I add a resource to class library?

In the 'FileStore' Class Library project, right-click the project folder and select Add > New Item. Select a new Resource file. I prefer to rename this to 'Resource.

How do I add a class library to an existing project?

On the main menu, you can click Build -> Build ProjectName. In the Solution Explorer, you can right-click the name of the project and click Build. In the Class View, you can right-click the name of the project and click Build.


3 Answers

This article explain how to use embedded resources in C#.

It boils down to

  1. At 'Design time': Add file to project and mark it as an embedded resource (properties)
  2. At 'Run time': Get instance of Assembly that holds the resource you want, then get that resource out as a stream.

var assembly = Assembly.GetExecutingAssembly();

var stream = assembly.GetManifestResourceStream("fully.qualified.name.of.the.resource");

If you're struggling to work out the fq name of the resource, a lazy way is to use the reflector to open the assembly that holds it. Go to the Resources folder, right click on the particular resource and choose 'Copy'.

like image 163
Frederik Avatar answered Sep 28 '22 08:09

Frederik


In windows application i would do it like ClassLibrary1.Properties.Resources.file.xml

By default, the class generated to access the resources is internal, so you can't access it from another assembly. If you need to make it public, select the .resx file, go to its properties, and change the Custom tool property from "ResXFileCodeGenerator" to "PublicResXFileCodeGenerator". This custom tool generates public classes, which should solve your problem.

like image 32
Thomas Levesque Avatar answered Sep 28 '22 08:09

Thomas Levesque


Right Click on Class Library Project, select properties, on tabs on the left choose Resources section.

Since your .resx files ares not recognizable by Visual Studio, add a new one. It will create a new .resx file.

Copy all your original content and paste into new one. Then you can delete old one. That's it!

You would need to repeat same process for your all supported languages.

like image 26
Teoman shipahi Avatar answered Sep 28 '22 09:09

Teoman shipahi