ASP.NET resources in separate assembly

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?

  1. First add that file in to class library project.
  2. Right click on that file and look at it’s properties.
  3. 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

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Reflection;
using System.IO;

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);
    Response.Write(streamreader.ReadToEnd());
  }
}

Description

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();

Tags:

Asp.net Interview Questions for 2 years or 3 years experianceHow to put Assembly In GACGAC Assemblyasp net image uploadASp.net 2.0 step by step Membership Provider could not load file or assemblyASP.net File HandlingCould not load file or assembly SystemServiceModelDomainServicesHosting Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 or one of its dependencies The system cannot find the file specifiedAsp.net Convert String To ImageWhat is web garden in Asp.net?Hack asp.net

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.