简体   繁体   中英

Write localized string to localized file

I have the following piece of code:

Dim fecha As DateTime = DateTime.Now
Using w As StreamWriter = File.AppendText(fichero.Text)
    w.WriteLine(fecha.ToString("dd/MM/yyyy") + vbTab + tiempo.Text + vbTab + comentario.Text)
    w.Close()
End Using

Which is called from a Windows Form in .NET 4, where tiempo and comentario are two TextBox where you get strings. The problem I have is that if I write a localized phrase with apostrophes, they appear as "ó" "é" or "Ã-" in the text file instead of "ó" "é" and "í".

Any idea how can I set the function to write correctly the localized phrase?

Instead of using File.AppendText you can create a new StreamWriter and specify whatever encoding you like. For example 1252 for Latin (Western European languages). 2'nd parameter specifies that the text should be appended to the file.

Using writer = New StreamWriter(fichero.Text, True, Encoding.GetEncoding(1252))
    writer.WriteLine(fecha.ToString("dd/MM/yyyy") + vbTab + tiempo.Text + vbTab + comentario.Text)
End Using

Also disposing the writer will call Close (no need to explicitly call it)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM