Website vs Web Application - Embedded resources

SheoNarayan
Posted by in ASP.NET category on for Beginner level | Views : 13849 red flag

In this small article, I am going to show the use of Build Action and Copy to Output Directory property of a file inside the class library and how it behaves differently in Web Site and Web Application projects.

As the title suggest, this article focus on the usefulness of Embedded Resources in asp.net projects. While working with Ajax controls and many other WYSIWYG controls, you must have seen that they frequenty uses images (like icons in the toolbars) and other related contents but these icons and contents file are not visible with their .dlls neither they exists in the bin folder. Now question arises where they come from? The answer is embedded resources. These images and conents are actually stored inside the .dll file as emedded resources that can be accessed using Reflection.

Build Action = "Content"

In Website project

  1. If you have a file (apart from .cs or .vb) in the class library with Build Action as Content and Copy to Output Directory as Copy Always even if the file is not copied in the output directory. In this case you need to copy the referenced file manually to your website directory. In this case, as the referenced file is not attached with the build/or not automatically copied to your output directory you will have to manually copy each time you have updated.
  2. You are free to copy your file (apart from .cs) in whatever folder you wish and you have to ensure that you are providing the right path to access the file.
Get solutions of the .NET problems with video explanations, .pdf and source code in .NET How to's.

In Web Application project

  1. If you have a file (apart from .cs or .vb) in the class library with Build Action as Content and Copy to Output Directory as Copy Always, the file is copied to the output directory when Web Application is built. Everytime you build, the referenced file is checked for any update and if updated since last edited, the new version of the file is copied.
  2. In this case as the file (apart from .cs or .vb) is copied to the bin folder, you can access that file as ~/bin/filename.html by spceifying the path of your project's bin folder.
Build Action = "Embedded Resource"

If you have a file (apart from .cs or .vb) in the class library with Build Action as Embedded Resource and Copy to output Directory as Do Not Copy, the file will not be copied to the output directory but as the Build Action property is Embeded Resources so the file will be embedded to the .dll of the class library and is available using reflection. This is applicable for all kinds of files like .jpg, .gif, .html, .txt etc. Many WYSIWYG controls use this technique to embed the toolbar buttons and other graphical elements.

To get the content of the file, you can use following code snippets in your class file inside the class library.

VB.NET Code

 Public Shared Function GetEmbeddedResourceContent(resourceName As String) As String
3:     Try
4:         Dim assembly As System.Reflection.Assembly = System.Reflection.Assembly.GetAssembly(GetType(Class1))
5:         Using stream As Stream = assembly.GetManifestResourceStream(assembly.GetName().Name + "." + resourceName)
6:             If stream = Nothing Then
7:                 Throw New Exception("Embedded resource " + resourceName + " doesn't exists")
8:             End If
9:             Dim bytes As Byte() = New Byte(stream.Length) {}
10:             stream.Position = 0
11:             stream.Read(bytes, 0, DirectCast(stream.Length, Integer))
12:                 ' this is your string
13:             Return Encoding.ASCII.GetString(bytes)
14:         End Using
15:     Catch ex As Exception
16:         Throw New Exception(ex.Message)
17:     End Try
18: End Function

C# Code

public static string GetEmbeddedResourceContent(string resourceName)

{

try

{

System.Reflection.Assembly assembly = System.Reflection.Assembly.GetAssembly(typeof(Class1));

using (Stream stream = assembly.GetManifestResourceStream(assembly.GetName().Name + "." + resourceName))

{

if (stream == null)

throw new Exception("Embedded resource " + resourceName + " doesn't exists");

byte[] bytes = new byte[stream.Length];

stream.Position = 0;

stream.Read(bytes, 0, (int)stream.Length);

return Encoding.ASCII.GetString(bytes); // this is your string

}

}

catch (Exception ex)

{

throw new Exception(ex.Message);

}

}

In above example, Class1 is the name of the class in which this code exists. You will have to use System.IO and System.Text namespace in order to use above code.

Note that to specify the path of the filename, we use . (dot) as separator instead of \ (back slash) that is used normally.

If your Embedded Files are .jpg or .gif or other types, you can easily modify the above code (remove the remaining lines from byte[] ...) and return the stream objects.

For example, if you have a EmbeddedHtmlFile.html file in your class library project with Build Action property as Embedded Resources, you can use following code snippet to get its contents.

string htmlContents = Class1.GetEmbeddedResourceContent("EmbeddedHtmlFile.html");

In this case my file EmbeddedHtmlFile.html is in the Class Library root folder not under any sub folder. htmlContents variable will have all the contents of the EmbeddedHtmlFile.html.

Hope this article will be useful for those who were not aware about Embedded Resource feature of the Class Library files.

Thanks for reading, happy reading !

Page copy protected against web site content infringement by Copyscape

About the Author

SheoNarayan
Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com
http://www.snarayan.com
Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)