Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 25104 |  Welcome, Guest!   Register  Login
 Home > Code Snippets > C# > How to get the content of Embedded file in assembly? ...
Poster

How to get the content of Embedded file in assembly?

 Code Snippet posted by: Poster | Posted on: 6/13/2009 | Category: C# Codes | Views: 2533 | Status: [Member] | Alert Moderator   
Ads

First, to make a file(like .xml, .html, .jpg or .gif) embedded into the assembly so that when building the project it is included into the .dll of the project, you need to go to Properties of the file and select "Build Action" property to "Embeded Resources".

To get the content of the Embedded resources into your class, you can write following code in C#.

        /// <summary>

/// Get conntent of the embeded resources
/// </summary>
/// <param name="resourceName"></param>
/// <returns></returns>
public static Stream GetEmbeddedResourceContent(string resourceName)
{
try
{
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetAssembly(typeof(Class1));
Stream str = assembly.GetManifestResourceStream(assembly.GetName().Name + "." + resourceName);

if (str == null)
throw new Exception("Could find the embedded resource '" + resourceName + "'");
return str;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}


In VB.NET, you can write following code.


''' <summary>

''' Get conntent of the embeded resources
''' </summary>
''' <param name="resourceName"></param>
''' <returns></returns>
Public Shared Function GetEmbeddedResourceContent(resourceName As String) As Stream
Try
Dim assembly As System.Reflection.Assembly = System.Reflection.Assembly.GetAssembly(GetType(Class1))
Dim str As Stream = assembly.GetManifestResourceStream(assembly.GetName().Name + "." + resourceName)

If str = Nothing Then
Throw New Exception("Could find the embedded resource '" + resourceName + "'")
End If
Return str
Catch e As Exception
Throw New Exception(e.Message)
End Try
End Function


Thanks
Found interesting? Add this to:


>> Write Response - Respond to this post and get points

More codes snippets

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/20/2013 4:01:04 AM