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

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
            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)
            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)
            success = True
        Catch ex As Exception
        End Try
        Return success
    End Function

Open in new window

Who is Participating?
Gary DavisConnect With a Mentor Dir Internet SvcsCommented:
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.
Gary Davis
11ptManAuthor Commented:
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.  
11ptManAuthor Commented:
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.
11ptManAuthor Commented:
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.
Gary DavisDir Internet SvcsCommented:
Great - I thought that was the answer but when you said both UTF8 and ASCII had the same results, I did not know why.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.