Click here to Skip to main content
Click here to Skip to main content

Embedding and Using Resources from .Net Assembly

, 4 Oct 2010
Rate this:
Please Sign up or sign in to vote.
.Net Assemblies can contain various types of resources like images, icons, files, etc. Such resources are mostly static, i.e.; do not keep changing during run time or application wise. Also, such resources are not executable items. So while deploying such assemblies, we need to make sure those resou

.NET Assemblies can contain various types of resources like images, icons, files, etc. Such resources are mostly static, i.e.; do not keep changing during run time or application wise. Also, such resources are not executable items. So while deploying such assemblies, we need to make sure those resources are intact with the packaged assemblies. Else, assemblies may blow up while executing the resource dependent methods. So as part of deployment strategy, we should embed such resources into the assembly itself.

Today we will see one such example of embedding Xml file into assembly.

  • Add one Class Library type project into the solution.
  • Add one Xml file as well. Fill Xml file with few data that the class library may use to query.
  • Right click on the Xml file, and select Properties –> Build Action. Out of several Build Action options, select Embedded Resource.

ResourceProperties

  • There you may see other properties as well like Copy to Output Directory. It has options like this:

ResourceSettings

  • If you select Do Not Copy, output of building the class library inside \bin\ folder will not have separate *.dll than embedded Xml file.
  • If you select Copy Always, output of building the class library inside \bin\ folder will always have *.dll and an embedded Xml file.

However, I prefer the first option. By this we make sure our distributable assembly is only one *.dll. This way the embedded resource Xml file cannot be modified, and our assembly can safely execute methods that depend upon this Xml file. I like to suggest one more tips here- rename this Xml file extension to *.config. Renaming Xml file extension to *.config makes file not browse able by Browser in web applications.

Here I have shown steps of embedding resource file into .Net assembly. This assembly can be either Class Library or Web application. We cannot embed resource file into web site type project as Web site does not produce assembly like Web application.

Now, let’s see how we can access embedded Xml file from assembly during runtime.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Xml;
using System.IO;

namespace MathLibrary
{
public class MathLibrary
{
//
private const string strFileName = “XMLFile.config”;
public System.Xml.XmlDocument GetXMLDocument()
{
//
var assembly = Assembly.GetExecutingAssembly();
var stream = assembly.GetManifestResourceStreamthis.GetType(), strFileName);
var doc = new XmlDocument();
try
{
if (stream == null)
{
throw new FileNotFoundException(“Couldnot find embedded mappings resource file.”, strFileName);
}
doc.Load(stream);
}
catch (Exception ex)
{
throw ex;
}
return doc;
}

public System.IO.Stream GetXMLStream()
{
//
var assembly = Assembly.GetExecutingAssembly();
var stream = assembly.GetManifestResourceStream(this.GetType(), strFileName);
try
{
if (stream == null)
{
throw new FileNotFoundException(“Couldnot find embedded mappings resource file.”, strFileName);
}
}
catch (Exception ex)
{
throw ex;
}
return stream;
}
}
}

There are two methods shown above- one returning XmlDocument object and other returning IO.Stream. Either of the return type can be used to construct XmlDocument object now at caller end of these methods.

Now let’s verify the assembly if it has any embedded resource or not. We can do this using either Reflector or Ildasm tool.

Reflector View:
ReflectorView

Ildasm View:
IldasmView

See resource name is qualified with assembly namespace name prefixed to it- MathLibrary.XMLFile.config.

Happy Coding! 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Dinesh K Mandal
Technical Lead Imfinity India Pte Ltd, Noida (Excelsoft Company)
India India
http://www.imfinity.com/

Comments and Discussions

 
QuestionCode is not working Pinmembervenkat sh19-Feb-14 19:33 
GeneralMy vote of 1 PinmemberRuwan Jayalath4-Feb-14 17:32 
GeneralMy vote of 5 Pinmemberaaroncampf8-Dec-11 7:32 
GeneralMy vote of 5 Pinmemberavikerchithi13-Jan-11 9:10 
GeneralMy vote of 5 Pinmembergkushner5-Oct-10 2:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 4 Oct 2010
Article Copyright 2010 by Dinesh K Mandal
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid