VB.Net .WriteLine(sometext) Prefixes Hidden Characters

I am writing to a .csv file using .WriteLine(sometext) and find that the first row appears to have hidden characters prefixed to it.
When inspecting the .csv in Excel or by notepad the first letter T is ok, however I believe there are hidden characters before the T. Inspecting the file with a HEX reader there is definitely some unprintable characters at that position.
This is the first line of the file.

The actual text is "T,9999999, ,Company Name,X,D," +
                                           Strings.Left(strDateEnded, 2) + Strings.Mid(strDateEnded, 4, 2).PadLeft(2, "0"c) + Strings.Right(strDateEnded, 2) + "," +
                                           Strings.Mid(strDateStarted, 4, 2).PadLeft(2, "0"c) & Strings.Right(strDateStarted, 2) & ",xxx01, ,"
TeDeSmAsked:
Who is Participating?
 
ste5anSenior DeveloperCommented:
Then you need to specify an encoding:

Imports System.IO
Imports System.Text

Module Module1
    Sub Main()
        Dim fileName As String = "C:\Temp\test.txt"
        Dim fileWriter As StreamWriter =
            My.Computer.FileSystem.OpenTextFileWriter(fileName, True, Encoding.ASCII)

        fileWriter.WriteLine("TEST")
        fileWriter.Close()

        Console.WriteLine("Done.")
        Console.ReadLine()
    End Sub
End Module

Open in new window

0
 
NorieVBA ExpertCommented:
How exactly are you writing to the CSV file?
0
 
Miguel OzSoftware EngineerCommented:
Which encoding are you using?
I think the characters you are referring are the BOM characters and you should leave them as is as they do not interfere with your file if you are reading/writing Unicode content CSV files.
The BOM character is visible only if you open the CSV in Excel, but not if you open it in a proper text editor (Scite, Notepad++ etc.).
if you are not planing to use Unicode files and only need English like contents the you should save files with Ascii encoding
1
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ste5anSenior DeveloperCommented:
In Addition to Miguel's post:

Do not use Excel to view CSV or other plain text data files. Cause Excel's "intelligence" will interfere, guess data types and apply fromats. Thus use always a only text editor.

And for .NET: As long as you don't specify explicitly different encodings when writing to files, .NET will use Unicode.
0
 
TeDeSmAuthor Commented:
I am using wrFileWriter = My.Computer.FileSystem.OpenTextFileWriter(strFileName, True) to write lines to the file. Additional lines are added within a loop. The file content needs to be ascii.

Lines are added using .WriteLine(strRow) where strRow is a line of text.
0
 
Miguel OzSoftware EngineerCommented:
I agree with ste5an, this is the implementation that my previous post recommends if you only need to save Ascii based text.
The key line is:
My.Computer.FileSystem.OpenTextFileWriter(fileName, True, Encoding.ASCII)

Open in new window

0
 
TeDeSmAuthor Commented:
This solution was more complete as the Imports statements were shown and proved to be necessary. Note that Visual Studio 2017 may flag the import statements as not necessary, they are! and they should be left in the code.
0
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.