Solved

How do I write and ANSI code page text file (ie.Codeset vs Encoding)  in VB.NET

Posted on 2009-07-10
5
1,028 Views
Last Modified: 2013-11-26
I'm working on an application that enables the user to modify the setting for a commercial application. The settings file for the comercial app shows to be a ANSI code page text file.
My application reads the file and populates a grid with its values. The user can then modify the values and save the file. The next time the commercial app runs it reads the modified setting file and executes accordingly. When the user of my application requests a save first a re-named copy of the original file is saved (the SaveMove routine does this) then the updated version of the file is created.

The issue I'm trying to resolve is that the file written by the routine below creates a UFT-8 txt file not an ANSI one. I read that by default .Net uses UTF-8 but I have not been able to determine how to make it write it in ANSI. I've tried changing the encoding when creating the file but it still gets written in UTF-8  Below is the routine that is called to write the file.

Can one of you tell me what I need to do to create an ANSI based text file and possibly explain the differance between code set and encoding in this context?
Public Function WriteMethod() As Boolean

        Dim success As Boolean = False

        Try

            Dim fi As New FileInfo(File)

            If fi.Exists Then SaveMove(fi) 'just to play save

            

            'The first version of this line had no encoding specified so .net used UFT-8

            'I tried both of the following lines with the same result. 

            Dim inifile As New System.IO.StreamWriter(File, False, System.Text.Encoding.Unicode)

            'Dim inifile As New System.IO.StreamWriter(File, False, System.Text.Encoding.ASCII)

            

            inifile.Write(m_Meth.Header.AllToString)

            Dim nrs As Int32() = m_Meth.GetAllStepNr()

            For Each n As Int32 In nrs

                inifile.WriteLine("[Step" & n.ToString() & "]")

                Dim hcs As HetCatStep = m_Meth.GetStepOf(n)

                inifile.Write(filterSpecialCharactersOut(hcs.AllToString))

            Next

            inifile.Close()

            success = True

        Catch ex As Exception

        End Try

        Return success

    End Function

Open in new window

0
Comment
Question by:11ptMan
  • 3
  • 2
5 Comments
 
LVL 18

Accepted Solution

by:
Gary Davis earned 500 total points
Comment Utility
If the file is an ascii text file, then UTF-8 is basically the same. All printable characters are a single byte (0x00 throuth 0x7f). It's different for special/foreign characters and may take more than 1 byte for them.
So basically, you can leave it the default UTF-8.
See http://en.wikipedia.org/wiki/UTF-8.
Gary Davis
0
 

Author Comment

by:11ptMan
Comment Utility
Gardavis, others

special caracters must be the issue.  Using a hex editor I've re-examined the two files. File A being the native settings file (ANSI) and File B being the modified version that I write using the code above.
The results are the following string SP, Evaporator (°C)=236.000000
Appears in File A (ANSI) as  53 50 2c 20 45 76 61 70 6F 72 61 74 6F 72 20 28 B0 43 29 3D 32 33 36 2E 30 30 30 30 30 30 0D 0A
Where File B (UTF-8) looks like this 53 50 2c 20 45 76 61 70 6F 72 61 74 6F 72 20 28 C2 B0 43 29 3D 32 33 36 2E 30 30 30 30 30 30 0D 0A

I am surmising that that the extra C2 byte at position 17 is what is causing the comercial app to fail to properly read it's settings file.   The ultimate fault may lie with the comercial app but I can't change it and need to make this work.   Since notepad still allows me to save as and specify ANSI format, the OS must still support it, so there has to be a way to write an ANSI text file with VB.NET.  
0
 

Author Comment

by:11ptMan
Comment Utility
Well,
I've resolved the issue. turns out that
Dim inifile As New System.IO.StreamWriter(File, False, System.Text.ASCIIEncoding.Default)
is the answer.  I tried this the other day but must have not had something right becasue then I still got UTF-8 today I get ANSI.  Still need to test the output file with the comercial app but I'm closing the question.
0
 

Author Closing Comment

by:11ptMan
Comment Utility
Gary,
While you response did not resolve my issue it did lead me to a great deal of information about the subject and a final solution so I'm giving you the points.
0
 
LVL 18

Expert Comment

by:Gary Davis
Comment Utility
Great - I thought that was the answer but when you said both UTF8 and ASCII had the same results, I did not know why.
Thanks,
Gary
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now