How can I detect the encoding/codepage of a text file

后端 未结 20 1383
梦如初夏
梦如初夏 2020-11-21 22:42

In our application, we receive text files (.txt, .csv, etc.) from diverse sources. When reading, these files sometimes contain garbage, because the

20条回答
  •  感情败类
    2020-11-21 23:10

    I use this code to detect Unicode and windows default ansi codepage when reading a file. For other codings a check of content is necessary, manually or by programming. This can de used to save the text with the same encoding as when it was opened. (I use VB.NET)

    'Works for Default and unicode (auto detect)
    Dim mystreamreader As New StreamReader(LocalFileName, Encoding.Default) 
    MyEditTextBox.Text = mystreamreader.ReadToEnd()
    Debug.Print(mystreamreader.CurrentEncoding.CodePage) 'Autodetected encoding
    mystreamreader.Close()
    

提交回复
热议问题