Write text files without Byte Order Mark (BOM)

I was working on a eLearning tool which supports various Learning Management Systems. Out of those one of the LMSs require files written in UTF8 encoding without Byte Order Mark(BOM).

If you are using Notepad++, you will find such type of encoding scheme over there. I was writing this piece of code in VB.Net.

The below code illustrates how to write files omitting BOM.

There seems to be a way of omitting the byte order mark (BOM) via passing False True to the UTF8Encoding constructor (link to MSDN reference page).
That is, use your own instance of UTF8Encoding instead of the default System.Text.Encoding.UTF8:
Dim utf8WithoutBom As New System.Text.UTF8Encoding(True)
                                                  '^^^^'
Using sink As New StreamWriter("Foobar.txt", False, utf8WithoutBom)
    sink.WriteLine("...")
End Using
(Note that omitting the BOM is only permissible for UTF-8, not for UTF-16.)

2 comments:

  1. Hey, thanks for the post.
    One comment, i'm doing this in PowerShell, and there i have to pass False (as you first indicated), in order to not get a BOM.

    Not sure if this is a mistake in your post, or a difference between language... but i'm guessing the .net calls would be the same...

    ReplyDelete
  2. Thankyou for this! Really, I was stumped.. I was getting those three pesky characters "" and had now way to remove them.. Turns out your code worked and you saved my bacon for sure.

    Was coding in Visual Baisc using OpenTextFileWriter and Write line..

    Looks like I will make my deadline in the end.. Thanks!

    ReplyDelete