I needed a program to convert some RTF files to plain text I seen a few but they did not do what I wanted however I decided to make my own little function. This code will take in a input RTF file and convert it to plain text as this is all I really wanted.

If I have time I try and make a proper GUI for this function.

Anyway use the code as you like hope it be of some use, Comments and suggestions welcome..

vbnet Code:
  1. Imports System.Text
  2. Imports System.IO
  3.  
  4. Public Class Form1
  5.  
  6.     Private Function rtf2txt(ByVal Source As String, ByVal Output As String) As Boolean
  7.         Dim rbox As New RichTextBox()
  8.         Dim sb As New StringBuilder()
  9.         Dim sw As StreamWriter
  10.  
  11.         If Not File.Exists(Source) Then
  12.             Return False
  13.         Else
  14.             'Open richtext source file.
  15.             rbox.LoadFile(Source, RichTextBoxStreamType.RichText)
  16.             'Append text to string builder.
  17.             sb.Append(rbox.Text).Replace(vbLf, vbCrLf)
  18.             Try
  19.                 'Create new output file.
  20.                 sw = New StreamWriter(Output)
  21.                 'Write plain text
  22.                 sw.Write(sb.ToString())
  23.                 'Close file.
  24.                 sw.Close()
  25.                 'Clear up
  26.                 sb = Nothing
  27.             Catch ex As Exception
  28.                 Return False
  29.             End Try
  30.         End If
  31.  
  32.         'Return good result.
  33.         Return True
  34.     End Function

vbnet Code:
  1. Private Sub cmdConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConvert.Click
  2.         'Example.
  3.         Dim Done As Boolean = rtf2txt("C:\out\data\Document.rtf", "C:\out\data\Document.txt")
  4.         'Just show user if convert was done ok.
  5.         MessageBox.Show("Convert ok = " & Done.ToString(), "rtf2txt", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  6.     End Sub