C# Question

Replacing string in txt using foreach

I'm trying to replace a string in a text file with everything there is in the other file. For my html Email variable.
But whenever i try to run the foreach, it gives me the error that it can convert char to string. How would one go about doing this in a different way?

StreamReader myreader = new StreamReader("VUCresult.txt");
StreamReader myreaderhtml = new StreamReader("htmlemail.html");
string lines = myreader.ReadToEnd();
string htmlmailbody = myreaderhtml.ReadToEnd();


if (lines == "Der er ikke nogen udmeldinger idag")
{
htmlmailbody.Replace("ingen", lines);
}
else
{
foreach (string s in lines)
{
htmlmailbody = htmlmailbody.Replace("Row2", s);
}
htmlmailbody = htmlmailbody.Replace("Row1", lines);
htmlmailbody = htmlmailbody.Replace("Row3", DateTime.Now.ToString());
}

Answer

You are using foreach over a very long string (that happens to include newlines); that will return you each individual character.

To get all the lines in a file (as a collection) just use File.ReadAllLines:

    string htmlmailbody;        
    using StreamReader myreaderhtml = new StreamReader("htmlemail.html"))
    {
       htmlmailbody = myreaderhtml.ReadToEnd();
    }
    string[] lines = File.ReadAllLines("VUCresult.txt");

    foreach (string s in lines)
    {
       ...
    }

Your original if check won't make sense here since you have a collection of lines instead of the entire file, and your second replace statements in the else won't make sense either. You need to decide which thing you are really trying to look at.