views:

110

answers:

2

How should I modify the following Vb.Net code to write str to the file in unicode?

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

Using sw As StreamWriter = New StreamWriter(fname)
    sw.Write(str)
    sw.Close()
End Using
+1  A: 

Use the overriden constructor to specify the encoding

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

Pick either UTF8(8bit) or Unicode(16 bit) character-set encoding as per your requirements.

NimsDotNet
+1  A: 

Documentation says that StreamWriter uses UTF8-Encoding by default.

Thorsten Dittmar
+1 Although it's UTF8 without BOM, so you might not realise!. http://msdn.microsoft.com/en-us/library/fysy0a4b.aspx
MarkJ