首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

openfiledialog打开的文件显示乱码解决办法

2012-02-28 
openfiledialog打开的文件显示乱码Public Class dialogsPrivate strfilename As StringPrivate Sub btnope

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)
测试文本文件通过

热点排行