Skip to main content

Tool to Convert Source Code to HTML Encoded Text in C#


It will be convenient to have a tool converting the source code to HTML encoded text, before I can post it on my blog article.

The following C# code is my tool to do the job:

public FormMain()
{
 InitializeComponent();
}

private void buttonExit_Click(object sender, EventArgs e)
{
 Close();
}

private void buttonRun_Click(object sender, EventArgs e)
{
 string src = textBoxSrc.Text;

 string html = System.Net.WebUtility.HtmlEncode(src);

 textBoxHTML.Text = html;

 textBoxHTML.SelectAll();

 textBoxHTML.Copy();
}

private void textBoxSrc_MouseClick(object sender, MouseEventArgs e)
{
 textBoxSrc.Text = "";
}

The UI:


Usage:

  1. Click on the textBoxSrc, the text in textBoxSrc will be cleaned. (line 26)
  2. Copy the source code to the textBoxSrc.
  3. Click the buttonRun.
  4. The converted text will be displayed on the textBoxHTML, in the same time, the text will also be copied to the clipboard for further use. (line 13 ~ 21)

You may use Visual Studio Community to create the project to adopt the code above and to create your tool.

Enjoy!

Comments