Help ! how to read a resource into a string

E

Elmue

Hello

What I want :
Embed a HTML file into my C# program and then
read the complete HTML resource into a string.

Can anybody tell me how to do that or better where I find a working sample ?

What I already did :
I added the HTML file in Visual Studio .NET to the solution via
Add --> Add Existing Item.

Under Properties I've set it to
Build Action = Embedded resource

When I look into the resulting EXE with an HEX editor I can see
that the HTML file has been compiled into the EXE file !

Nice !
But how do I read it into a string ?
I searched for hours without success !

Elmü
 
J

Jon Skeet [C# MVP]

Elmue said:
What I want :
Embed a HTML file into my C# program and then
read the complete HTML resource into a string.

Can anybody tell me how to do that or better where I find a working sample ?

What I already did :
I added the HTML file in Visual Studio .NET to the solution via
Add --> Add Existing Item.

Under Properties I've set it to
Build Action = Embedded resource

When I look into the resulting EXE with an HEX editor I can see
that the HTML file has been compiled into the EXE file !

Nice !
But how do I read it into a string ?
I searched for hours without success !

Find out your current assembly with Assembly.GetExecutingAssembly().
Call Assembly.GetManifestResourceStream() to get a stream.
Wrap it in a StreamReader, and call ReadToEnd().
 
E

Elmue

Hello

Thanks for your answer !
I would never have found that.

But an important information was missing :
What exactly to pass to GetManifestResourceStream(string) ?

I found it now and here is the code if someone else needs it :

// how to read an embedded resource with the filename "Template.htm" into a string:
using System.Reflections;using System.IO;
Assembly Assm = Assembly.GetExecutingAssembly();Stream MyStream = Assm.GetManifestResourceStream("MyProject.Resources.Template.htm");StreamReader StrmRead = new StreamReader(MyStream);string HtmTempl = StrmRead.ReadToEnd();

Elmü
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top