Create txt file with encoding of ANSI
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
12-07-21 10:45 AM
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.
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
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
12-07-21 12:14 PM
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
See this article for some examples:
https://www.arclab.com/en/kb/csharp/read-write-text-file-ansi-utf8-unicode.html
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
14-07-21 04:39 AM
Thank you, Eric Wilson. It works in my process.
