1

Here are two methods for writing text to a file in VB.Net 2012. The first one prepends the same three non-printable characters to each file: . The second one works as expected and does not add the three characters. objDataReader is an OleDB datareader.

Any idea why?

Greg

My.Computer.FileSystem.WriteAllText(lblLocation.Text & "\" & 
objDataReader("MessageControlId").ToString & ".txt", objDataReader("MsgContents").ToString, False)

Using outfile As New StreamWriter(lblLocation.Text & "\" & objDataReader("MessageControlId").ToString & ".txt")
    outfile.Write(objDataReader("MsgContents").ToString)
End Using
user1091524
  • 865
  • 1
  • 15
  • 28
  • 2
    It is the BOM, it says that the rest of the file contains utf-8 encoded text. A BOM is a Good Thing, it is entirely normal and any modern text editor knows how to deal with it. VS certainly does. Sadly, StreamWriter uses utf-8 but does not write the BOM by default. You'll have to add UTF8Encoding as the second argument to the constructor to get it to do the right thing. – Hans Passant Feb 24 '14 at 19:30

1 Answers1

2

Thanks. I found the entry below I after Googled BOM, in case anyone wants a more detailed explanation. While the BOM was not visible in a text editor it did cause problems when I passed the file to our HL7 interface engine.

Greg

Write text files without Byte Order Mark (BOM)?

Community
  • 1
  • 1
user1091524
  • 865
  • 1
  • 15
  • 28