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, ,"
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
How exactly are you writing to the CSV file?
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
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.
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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.
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)


    End Sub
End Module

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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

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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.