Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2009-07-10
5
Medium Priority
?
1,042 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 18

Accepted Solution

by:
Gary Davis earned 2000 total points
ID: 24831855
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
ID: 24843397
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
ID: 24844792
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
ID: 31602213
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
ID: 24850115
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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Screencast - Getting to Know the Pipeline

610 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