C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Training   ASP.NET Web Hosting    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

Play Silverlight Games or Submit your Silverlight applications and earn 90% AdSense revenue.

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Resources » Articles » .NET Framework »

Working with embedded resource in dot net assembly.


Posted Date: 19 May 2004    Resource Type: Articles    Category: .NET Framework
Author: ManojRajanMember Level: Gold    
Rating: Points: 7



Dot net assembly can store embedded resources like text information and images required for your application. Normally we read/access text/image files from the hard disk. But some time you want the application to include these external resource file which can be stored in the assembly itself, so that whenever you deploy the application, you need not worry about integrating all required files in the setup. Let us first see how to include these files in the assembly.

Open a new project.
In the solution explorer right click on the project name.
Click on Add Existing Item.
Change the file type drop down in the select file dialog box to the desired extension, and then select all the files that you require.
Now right click on the added files and select properties
You can see the default value of the Build Action property as “Content”.
Change it to Embedded Resource.


Now the files are added as part of the assembly.
Let us see how you can access it.
Suppose you have added one image file named 1.bmp and 1 text file named test.txt


Let us read the image file and the text file on the form load event.


Dim executing_assembly As System.Reflection.Assembly = _
Me.GetType.Assembly.GetEntryAssembly()

' Get the namespace.
Dim my_namespace As String = _
executing_assembly.GetName().Name.ToString()


‘Load image to a dynamically created picture box.

Dim picture_stream As Stream
Dim bm As Bitmap

picture_stream = _
executing_assembly.GetManifestResourceStream(my_namespace _
+ ".1.bmp")

If Not (picture_stream Is Nothing) Then
bm = New Bitmap(picture_stream)
Dim pict As New PictureBox()
pict.Image = bm
pict.Top = 5
pict.Left = 5
pict.Height = 50
pict.Width = 50
pict.Visible = True
Me.Controls.Add(pict)
picture_stream.Close()
End If



‘Load text to a dynamically created Label.

Dim text_stream As Stream = _
executing_assembly.GetManifestResourceStream(my_namespace _
+ ".Test.txt")
If Not (text_stream Is Nothing) Then
Dim stream_reader As New StreamReader(text_stream)
Dim lbl As New Label()
lbl.Text = stream_reader.ReadToEnd()
lbl.Top = 75
lbl.Left = 5
lbl.Width = 500
lbl.Height = 500
lbl.Visible = True
Me.Controls.Add(lbl)
stream_reader.Close()
End If




Responses

Author: shubie    08 Jun 2004Member Level: Bronze   Points : 0
thanks very much for sharing it with us. just wondering is there a way I can embed image file in my composite control. for example I have created a custom server control in vb.net and need to show a image if the control is readonly, at the moment I can do it but I cant embed them in the assembly. by going through your code I can use it in picture box but how do I assign it to a imagebutton.imageurl property.
any advice would be most appreciative.
thanks again for your time


Author: ManojRajan    08 Jun 2004Member Level: Gold   Points : 0
You can store the Image in the assembly and because you read it using a sreamreader, you must assign the value to some picture box control. if you want to use the image in an image button control, the first thing you need is to save the bitmapimage in some file and give the path of this file in the imageurl.


Author: shubie    08 Jun 2004Member Level: Bronze   Points : 0
Thank you for the prompt reply.but this will make me attach the file as well. I just need to import the dll without any files. I have read it somewhere theat whidbey will have this functionality. agaian thank you for your time and effort.


Author: jeremy    26 Jul 2004Member Level: Bronze   Points : 0
Can this be done with an MS Access .mdb file that the application uses (read/write)?

I'm trying to imagine exactly how that wouold work in this context. Can't see it clearly in my mind.

Any thoughts would be greatly appreciated.

- Jeremy




Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Some Issues on namespaces
Previous Resource: Compiling source files and building assemblies from command prompt
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use