RichTextBox不会显示'并显示菱形,而是在其中显示问号

时间:2016-03-20 13:10:34

标签: .net vb.net text encoding streamreader

图片显示了下面的文字: -

enter image description here

以下是更新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)

1 个答案:

答案 0 :(得分:2)

这是一个关于错误编码的问题,请使用指定文本编码的StreamReader重载:

您需要确保使用正确的编码,与文本文件编码相同。

按照您提供的代码示例:

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