ASP.NET resources in separate assembly
There are situations where you need to read resources from separate assembly. In .net you can use System.Reflection namespace to load assembly and read resources and other information.
I have created a class library project called Reven.Core, it consists of embedded resource Demo.txt.
How to embedded a resource in assembly?
- First add that file in to class library project.
- Right click on that file and look at it’s properties.
- Set Build Action property of that file to Embedded Resource.
In my Reven.Core dll I have demo.txt. I have added reference of Reven.Core to my web application.
On button click event I’m reading the content of demo.txt.
Code To read embedded resources
public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
protected void Button1_Click(object sender, EventArgs e)
Stream stream = Assembly.Load("Reven.Core").GetManifestResourceStream("Reven.Core.Demo.txt");
StreamReader streamreader = new StreamReader(stream);
Assembly.Load is static method of Assembly class. Here we are loading Reven.Core assembly.
GetManifestResourceStream() accepts fully qualified name of resource.
For example: “Reven.Core.Demo.txt”
You may want to read all resources embedded in assembly. To do that you can refer below code.
It will return array of sting containing all resources fully qualified name.
string arrResources = Assembly.Load("Reven.Core").GetManifestResourceNames();