cancel
Showing results for 
Search instead for 
Did you mean: 

Create txt file with encoding of ANSI

EricLi
Level 6
Our current process is create a txt file, and then write into data save as a XML file. Target application read this xml file for further process. 

I am using codes to create a txt file, the file is defaulted to UTF-8, in fact I have no idea why it is defaulted to that. below is my codes.

Try

Dim sw As StreamWriter

If File.Exists(File_Name) = False Then
sw = File.CreateText(File_Name)
End If


Success = True
Message = ""
Catch e As Exception
Success = False
Message = e.Message
End Try


But recently the target application get upgraded, the acceptable encoding is ANSI instead of UTF-8 any more, else Chinese characters will be not recognized. 

I am thinking to create a txt object to change the encoding, but it looks inefficient. I want to know how to create the txt file with ANSI encoding based on my codes.
2 REPLIES 2

ewilson
Staff
Staff
Unicode, and UTF-8, is the default within .NET. If you want ASCII, you’ll need to specifying that on your StreamWriter using the Encoding interface.

See this article for some examples:

https://www.arclab.com/en/kb/csharp/read-write-text-file-ansi-utf8-unicode.html

EricLi
Level 6
Thank you, Eric Wilson. It works in my process.