0

I'm freaking out with this problem. I want to read the data of a HTML in c#. So, I have an HTML file in the same Class Library in Visual Studio. Visual Studio Class Library distribution

So, I want to use Stream Reader to read the file, with the assembly.

StreamReader reader = new StreamReader(assembly.GetManifestResourceStream("proyectoHADS.LIbreriaClase.EmailTemplate.html"));

But, this returns a null, so I use the GetManifestResourceNames to check if it exists, but it doesn't because the array is empty. I don't know why I can't get the HTML. The entire code is here:

namespace LibreriaClase

public class EnviarCorreo { 

    public void enviarCorreo(String correo)
    {
        ....

        var assembly = Assembly.GetExecutingAssembly();
        string[] names = assembly.GetManifestResourceNames();
        StreamReader reader = new StreamReader(assembly.GetManifestResourceStream("proyectoHADS.LIbreriaClase.EmailTemplate.html"));
        body = reader.ReadToEnd();`
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Does the file have to be part of the class library? – miro99 Feb 11 '20 at 20:17
  • 2
    Does this answer your question? [Can't load a manifest resource with GetManifestResourceStream()](https://stackoverflow.com/questions/3068736/cant-load-a-manifest-resource-with-getmanifestresourcestream) – Tom W Feb 11 '20 at 20:23
  • 1
    Is the file set as EmbeddedResource? – IS4 Feb 11 '20 at 23:57

1 Answers1

0

I hope this code snippet can help you:

public void enviarCorreo(String correo)
{        
  ....
var assembly = Assembly.GetExecutingAssembly();
string[] names = assembly.GetManifestResourceNames();
using( StreamReader reader = new StreamReader(assembly.GetManifestResourceStream("proyectoHADS.LIbreriaClase.EmailTemplate.html"));
{
 String line = String.Emtpy;
 while( (line = reader.ReadLine()) != null )
 {
  Console.WriteLine(line);
 }
}

By the way dont forget check proyectoHADS.LIbreriaClase.EmailTemplate.html line is it null or not.

Halil Sahin
  • 568
  • 1
  • 6
  • 25