advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Download the code for this article
This is a cool technique. Were you aware of the capability for embedding files in .NET assemblies before reading this article? Do you already use the technique or do you plan to now? If you have any questions or problems embedding files in your .NET assemblies, let us know in the vb.dotnet.technical discussion group.
Partners & Affiliates
advertisement
advertisement
advertisement
advertisement
Average Rating: 4.2/5 | Rate this item | 6 users have rated this item.
How to Embed Resource Files in .NET Assemblies (cont'd)
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.
advertisement


Examples
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.

Previous Page: Storing Files as Resources in an Assembly  


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 .
Page 1: IntroductionPage 3: Build the Sample Application
Page 2: Storing Files as Resources in an Assembly 
Please rate this item (5=best)
 1  2  3  4  5
advertisement