To install ColorCode you can use NuGet command or add ColorCode reference as a library downloadable from Codeplex. NuGet command:
PM> Install-Package ColorCode
Only Microsoft stack languages are supported by ColorCode. Here is the complete list:
- ASPX (C#)
- ASPX (VB.NET)
As I mentioned earlier that this library uses in-line CSS and therefore you cannot use it in the windows forms application. You can use the webbrowser control in windows forms application if you want code to render correctly.
This is a sample basic usage of ColorCode.
string code = File.ReadAllText(Server.MapPath("~/Test.txt")); string finalCode = new CodeColorizer().Colorize(code, Languages.CSharp); codediv.InnerHtml = finalCode;
First line of the code will read all the text from a file. The second line will initialize a new
CodeColorizer class Colorize method which will take 2 parameters, first is the code for which we want the syntax highlighting for and the second parameter is language. I now have the
finalCode string which has the code with syntax highlighting. This is the output I have in my browser window.
It looks pretty good from the front but behind the scenes it’s not that pretty.
ColorCode is an open-source library and is hosted on Codeplex.