I have a problem i set the streawriter to write ISO-8853-1 but the file gets utf8 encoding. See code below:
when declaring str i set it to iso-8853-1 and when i check the output files encoding i get UTF8-without bom.
Imports System.Text
Imports System.IO
Module Module1
Sub Main()
Using str = New StreamWriter("C:\blabla.txt", False, encoding.GetEncoding("ISO-8859-1"))
str.Write("23124124AÖ")
Dim encoding = str.Encoding
str.Close()
End Using
Dim currentEncoding = GetFileEncoding("C:\blabla.txt")
End Sub
Public Function GetFileEncoding(filePath As String) As Encoding
Using sr As StreamReader = New StreamReader(filePath, True)
sr.Read()
Return sr.CurrentEncoding
End Using
End Function
End Module
There's a reason that you have to pass encodings to the constructors of both readers and writers. Files, in and of themselves, don't have encodings. They're just a collection of bytes.
It's up to you, be selecting an encoding, to say what interpretation you want to place on those bytes. You opened the StreamReader
without specifying an encoding and so it defaults to UTF-8.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments