Welcome to Dream.In.Code
Become a C# Expert!

Join 150,411 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 974 people online right now. Registration is fast and FREE... Join Now!




Embedded Resource Files

 
Reply to this topicStart new topic

Embedded Resource Files

PsychoCoder
27 Mar, 2008 - 05:16 PM
Post #1

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
This one is driving me crazy. I have 4 text files that I have marked as Embedded Resources. I am trying to read one of the text files with this code


csharp

public static string GetMailTemplate(string name)
{
Assembly assembly = Assembly.GetExecutingAssembly();
string contents = string.Empty;
try
{
Stream strm = assembly.GetManifestResourceStream(assembly.GetName().Name.ToString() + "." + name);
StreamReader reader = new StreamReader(strm);
while (reader.Peek() != -1)
{
contents += reader.ReadLine();
}
reader.Close();
return contents;
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
return string.Empty;
}
catch (ArgumentNullException ex)
{
Console.WriteLine(ex.Message);
return string.Empty;
}
}



When I run it I get an ArgumentNullException on this line


csharp

Stream strm = assembly.GetManifestResourceStream(assembly.GetName().Name.ToString() + "." + name);



I am calling the above method like so


csharp

Console.WriteLine(TasksUtilities.GetMailTemplate("MailMessageTemplate14Days.txt"));



If you look at the screenshot below the files are definitely in the project


Attached Image


The only difference between this and other times Ive read embedded resources is this is a console application, but I wouldn't think that would make a difference?
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Embedded Resource Files
27 Mar, 2008 - 06:59 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 314 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
It is in your resources folder... the assembly path should be assembly.GetName().Name.ToString() + ".Resources." + name

That should get you back up and running. smile.gif
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Embedded Resource Files
27 Mar, 2008 - 08:28 PM
Post #3

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
How can I be so stupid. Thanks Marty!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 07:41PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month