Posted by: wajahatabbas | February 7, 2005

Accessing Embedded Recources From Assebmly

Dear All..

We will see here, how to access embedded recourses of an Assembly.

Like, if you have designed some html pages, and you want to show, consume in your page (asp.net), or any template, any text file, you just want to consume that file into your page.

A typical scenario is the one, in which your program needs to email html messages, which can modified and changed,,, you want to consume those html pages in your email.

Create a Vb.Net windows project, named it ReadingAssemblies, add a html page and name it MyPage.htm, in this html page, write anything,, like “hello World “(Most formal start :)) After that click on the file and in the properties window, modify the Build Action with Embedded Recourse because this will allow your page to embed as a recourse which can be accessed consuming the assembly .Now this file will be a part of your ReadingAssemblies, Add a form , add a button and a richtextbox in your form. Add the following code in your Button Click event,

‘*********************************************************************
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim execAssembly As [Assembly] ‘ Taking instance of Assembly
execAssembly = [Assembly].GetExecutingAssembly ‘ Getting the Executing Assembly (own)


Dim iStream As Stream ‘ Getting whole stream of Html page in a stram object
iStream = execAssembly.GetManifestResourceStream(“ReadingAssemblies.MyPage.htm”)

Dim bStream As New StreamReader(iStream) ‘ Reading the stream object
rchTextBox.Text = bStream.ReadToEnd ‘ Showing the whole file in a richtextbox

End Sub

‘*********************************************************************

So you have seen that how simple to access any file. We just created a html page, change it build action, and then consume it…

Its one of the simplest way to consume embedded resource.

.Nitting,
Wajahat Abbas

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: