openfiledialog打开的文件显示乱码
Public Class dialogs
Private strfilename As String
Private Sub btnopen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnopen.Click
With OpenFileDialog1
.Filter = "text document(*.txt)|*.txt|all file(*.txt)|*.*"
.FilterIndex = 1
.Title = " demo open file dialog"
End With
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
strfilename = OpenFileDialog1.FileName
Dim fileContents As String
fileContents = My.Computer.FileSystem.ReadAllText(strfilename)
txtfile.Text = fileContents
Catch ex As Exception
MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
End Class
打开的文本文件是乱码 是怎么回事 啊??
[解决办法]
在ReadAllText方法的FILENAME参数后面指定ENCODING
[解决办法]
ls的意思是
fileContents = My.Computer.FileSystem.ReadAllText(strfilename)
改成
fileContents = My.Computer.FileSystem.ReadAllText(strfilename, System.Text.Encoding.Default)
测试文本文件通过