You may find it more desirable to create HTML in an HTML file rather than concatenating strings. Try the following:
Create a Windows Forms App (.NET Framework)
VS 2019:
Open Solution Explorer
- In VS menu, click View
- Select Solution Explorer
Open Properties Window
- In VS menu, click View
- Select Properties Window
Add folder (name: Html)
- In Solution Explorer, right-click <project name>
- Select Add
- Select New folder. Rename to desired name (ex: Html)
Add HTML file
- In Solution Explorer, right-click the folder you created (ex: Html)
- Select Add
- Select New Item...
- On left side, under Visual C# Items, click Web
- Select HTML page (name: HTMLPage1.html)
- Click Add
- In Solution Explorer, click HTML page (ex: HTMLPage1.html)
- In Properties Window, set the following property: Build Action: Embedded Resource
Create a class (name: HelperLoadResource.cs)
Add the following using statements:
using System;
using System.Text;
using System.IO;
using System.Reflection;
HelperLoadResource
Note: The following code is from this post.
public static class HelperLoadResource
{
public static string ReadResource(string filename)
{
//use UTF8 encoding as the default encoding
return ReadResource(filename, Encoding.UTF8);
}
public static string ReadResource(string filename, Encoding fileEncoding)
{
string fqResourceName = string.Empty;
string result = string.Empty;
//get executing assembly
Assembly execAssembly = Assembly.GetExecutingAssembly();
//get resource names
string[] resourceNames = execAssembly.GetManifestResourceNames();
if (resourceNames != null && resourceNames.Length > 0)
{
foreach (string rName in resourceNames)
{
if (rName.EndsWith(filename))
{
//set value to 1st match
//if the same filename exists in different folders,
//the filename can be specified as <folder name>.<filename>
//or <namespace>.<folder name>.<filename>
fqResourceName = rName;
//exit loop
break;
}
}
//if not found, throw exception
if (String.IsNullOrEmpty(fqResourceName))
{
throw new Exception($"Resource '{filename}' not found.");
}
//get file text
using (Stream s = execAssembly.GetManifestResourceStream(fqResourceName))
{
using (StreamReader reader = new StreamReader(s, fileEncoding))
{
//get text
result = reader.ReadToEnd();
}
}
}
return result;
}
}
Usage:
//get HTML as string
string html = HelperLoadResource.ReadResource("HTMLPage1.html");
Resources: