Easily Convert a String to RTF in .NET

I came across a situation where I needed to convert a string into RTF format dynamically in an application. After some searching for a .NET RTF library or a conversion function, I decided to just implement my own find/replace method. Needless to say, this proved to be rather tricky. An then, the ah ha moment… I got a tip from a colleague to use the WinForms RichTextBox control and let it do the work. After a little testing, it seems to work pretty well so I figured I’d share this snippet if you ever have the same issue.

private static string FormatAsRTF(string DirtyText)
    System.Windows.Forms.RichTextBox rtf = new System.Windows.Forms.RichTextBox();
    rtf.Text = DirtyText;
    return rtf.Rtf;