2 Replies - 17686 Views - Last Post: 27 March 2008 - 09:28 PM Rate Topic: -----

#1 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Embedded Resource Files

Posted 27 March 2008 - 06:16 PM

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


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


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




I am calling the above method like so


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?

Is This A Good Question/Topic? 0
  • +

Replies To: Embedded Resource Files

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5332
  • View blog
  • Posts: 14,211
  • Joined: 18-April 07

Re: Embedded Resource Files

Posted 27 March 2008 - 07:59 PM

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. :)
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Embedded Resource Files

Posted 27 March 2008 - 09:28 PM

How can I be so stupid. Thanks Marty!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1