How to write a file in Unicode in Vb.Net

How do I change the following Vb.Net code to write str to a file in unicode?

Do I need to convert str to Unicode before writing to a file?

 Using sw As StreamWriter = New StreamWriter(fname) sw.Write(str) sw.Close() End Using 
+6
unicode streamwriter
source share
3 answers

Use override constructor to specify encoding

 Using sw As StreamWriter = New StreamWriter(fname, true, System.Text.Encoding.Unicode) sw.Write(str) sw.Close() End Using 

Select the character encoding UTF8 (8 bits) or Unicode (16 bits) according to your requirements.

+8
source share

The documentation says that StreamWriter uses UTF8 encoding by default.

+2
source share

The code below explicitly states how to save UTF-8 without specification.

 Dim utf8WithoutBom As New System.Text.UTF8Encoding(False) Dim orfWriter As System.IO.StreamWriter = New System.IO.StreamWriter(fileName, append, utf8WithoutBom) orfWriter.Write(saveString) orfWriter.Close() 

For full documentation see www.ezVB.net .

0
source share

All Articles