Search code examples
c#htmloophtmltextwriter

C# - object-oriented way of writing HTML as a string?


I'm trying to programmatically send an email of all of the dll files and their versions in a directory, recursively. I'd like to send the email as HTML output, using a table. Is there a good object-oriented way of doing this? I'd hate to write all of the tags by hand.

Something like:

private string getHTMLString()
{
    DirectoryInfo di = new DirectoryInfo("some directory");
    FileInfo[] files = di.GetFiles("*.dll", SearchOption.AllDirectories);
    foreach (FileInfo file in files)
    {
        Assembly assembly = Assembly.LoadFile(file.FullName);
        string version = assembly.GetName().Version.ToString();
    }
 }

Solution

  • Something like this?

    private string getHTMLString()
    {
        DirectoryInfo di = new DirectoryInfo("some directory");
        FileInfo[] files = di.GetFiles("*.dll", SearchOption.AllDirectories);
        StringBuilder sb = new StringBuilder();
        sb.Append("<table>");
        foreach (FileInfo file in files)
        {
            Assembly assembly = Assembly.LoadFile(file.FullName);
            string version = assembly.GetName().Version.ToString();
            sb.AppendFormat("<tr><td>{0}</td><td>{1}</td></tr>", file.FullName, version);
        }
        sb.Append("</table>");
        return sb.ToString();
    
     }
    

    Not really "object oriented" but I would argue the most logical.

    DISCLAIMER: Compiled by hand