Loading Embedded Resources

R

Ryan Ramsey

I currently have an RTF file added to my project as an embedded resource.

I am then using the following code to read it into a richtextbox:

Stream stream =
this.GetType().Assembly.GetManifestResourceStream("DKPCalc.doc.rtf");

if (stream != null) {
StreamReader sr = new StreamReader(stream);
richTextBox1.LoadFile(stream, RichTextBoxStreamType.RichText);
sr.Close();
}else
MessageBox.Show("Could not load resource.");

Whenever I try to use this, 'stream' is returned as a NULL.

do a:

Stream stream =
this.GetType().Assembly.GetManifestResourceStream("Resources/doc.rtf");

the text will be loaded in debug mode but of course fails when the app is
distributed because I don't distribute the rtf..

I have read several whitepapers on MSDN and have tried
http://msdn.microsoft.com/library/d...nsreflectionpermissionattributeclasstopic.asp
but have met no success.



Any help would be appreciated.
 
G

Guest

It seems that you haven't included app's namespace in resource name. Do so, I
think it must be the solution.
 
R

Ryan Ramsey

The namespoace is called DKPCalc
the filename is doc.rtf

Do I have this entered incorrectly?

Stream stream =
this.GetType().Assembly.GetManifestResourceStream("DKPCalc.doc.rtf");
 
G

Guest

Ryan,
I tried your code(except with my own namespace and filename:
"windowsapplication2.my.rtf") with absolutely no error. maybe you are not
using the correct case of letters, since the resource name is case-sensitive.
 
S

Stephen Ahn

Ryan,

If you are still having problems, I'd suggest downloading a tool like dotnet
Reflector

(freely available here : http://www.aisto.com/roeder/dotnet/ )

You could then load your assembly into Reflector, then see if the embedded
resource is really there, and confirm the full namespace path of the
resource.

HTH,
Stephen
 
R

Ryan Ramsey

This helped TREMENDOUSLY....

It turns out that I didn't have "Resources" in the string.
Stream stream =
this.GetType().Assembly.GetManifestResourceStream("DKPCalc.Resources.doc.rtf");
 

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