Search code examples
.netvb.nettextencodingstreamreader

RichTextBox does not display ' and shows up a diamond instead with a question mark inside it


The picture show how the text appears below :-

enter image description here

Below is the code to update the RichTextBox

If File.Exists(C_Drive + "\compu\update.txt") Then
    Dim sr As StreamReader = New StreamReader(C_Drive + "\compu\update.txt")

    ' Read and display the lines from the file until the end 
    ' of the file is reached.
    RichTextBox1.Text = RichTextBox1.Text + sr.ReadToEnd
    sr.Close()
End If

RichTextBox1.Select(0, 0)

Solution

  • It is an issue about wrong encoding, use the StreamReader overload that specifies a text encoding:

    You need to ensure to be using the right encoding, the same as the textfile encoding has.

    Example following the code that you've provided:

    Dim textfile As New FileInfo(String.Format("{0}\compu\update.txt", C_Drive))
    
    If textfile.Exists() Then 
        Using sr As New StreamReader(textfile.FullName, Encoding.UTF8)
            RichTextBox1.Append(sr.ReadToEnd())
        End Using
    End If