Browse DevX
Sign up for e-mail newsletters from DevX


How to Embed Resource Files in .NET Assemblies  : Page 3

Use .NET's ability to embed XML files, icons, graphics—anything you want—directly into your assemblies.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Build the Sample Application
The sample application for this article consists of a simple Windows Forms application that contains three embedded files: an icon, an XML data file and an XSL style sheet. The following code excerpts demonstrate how to access the embedded resources at runtime.

Reading an embedded icon

' The GetIcon method centralizes the process to ' retrieve icon resources ' method to retrieve and assign an icon Sub ShowIcon picIcon.Image = GetIcon("Icon1.ico").ToBitmap End Sub ' method to retrieve an embedded icon resource Private Function GetIcon(ByVal strIdentifier As String) As System.Drawing.Icon ' use the strIdentifier argument to retrieve the ' appropriate resource from the assembly With New System.IO.StreamReader( _ [Assembly].GetEntryAssembly. _ GetManifestResourceStream(strIdentifier)) ' read the resource from the returned stream GetIcon = New System.Drawing.Icon(.BaseStream) ' close the stream .Close() End With End Function

Reading an embedded XML file

Sub GetListData Dim xmlListData As Xml.XmlDocument Dim xmlItem As Xml.XmlNode ' The GetXML method centralizes the process to ' retrieve XML resources xmlListData = GetXML("listdata.xml") For Each xmlItem In xmlListData.DocumentElement.ChildNodes lstData.Items.Add(xmlItem.InnerText) Next End Sub ' retrieve an embedded XML file Private Function GetXML(ByVal strIdentifier As String) As System.Xml.XmlDocument Dim xmlDoc As New System.Xml.XmlDocument() ' use the strIdentifier argument to retrieve the ' appropriate resource from the assembly With New System.IO.StreamReader( _ [Assembly].GetEntryAssembly. _ GetManifestResourceStream (strIdentifier)) ' load the document from the returned stream xmlDoc.Load(.BaseStream) .Close() ' return the document GetXML = xmlDoc End With End Function

Reading and Parsing an Embedded XSLT Stylesheet

Sub ParseData Dim stmOutputStream As New System.IO.MemoryStream() ' The GetStylesheet method centralizes the process ' to retrieve XSLT stylesheets, returning them as an ' instance of the XslTransform class. GetStylesheet("sample.xslt").Transform( _ GetXML("listdata.xml"), Nothing, stmOutputStream) txtResults.Text = _ System.Text.Encoding.ASCII.GetString( _ stmOutputStream.ToArray) stmOutputStream.Close() End Sub Private Function GetStylesheet(ByVal strIdentifier As String) As System.Xml.Xsl.XslTransform Dim xslSheet As New System.Xml.Xsl.XslTransform() Dim xslReader = _ New Xml.XmlTextReader( _ Assembly].GetEntryAssembly. _ GetManifestResourceStream(strIdentifier)) xslSheet.Load(xslReader) xslReader.Close() GetStylesheet = xslSheet End Function

Using Visual Studio, embedding files as runtime-accessible resources is easy. Embedding resources within your assembly can help to protect your intellectual property, prevent users from altering application resources and can reduce application deployment difficulties.

Anthony Glenwright is Product Development Manager at Inventua. Inventua creates tools to manage the workflows of running a successful software organization and general-purpose tools designed to improve software quality. You can contact him via the website at http://www.inventua.com. or reach him directly by email by .
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date