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

Labels

.net .Net Instrumentation logging .net localization Agile amazon amazon elasticache amazon services AppDomain Application Domain architecture asp ASP.Net authentication authentication mechanisms Byte order mark c# cache canvas app cdata certifications class classic mode cloud cloud computing cluster code-behind Combobox compilation Configuration providers configurations connection connectionString constructors control controls contructor CSV CTS .net types conversion database DataGridView DataSource DataTable DataType DBML delegates design pattern dispose double encoding Entity framework Events exception handling expiry fault contracts fault exceptions function pointers functions generics help HostingEnvironmentException IIS inner join instance management integrated mode javascript join left outer join LINQ LINQ join LINQ to SQL memory leak methods microsoft model driven app modes in IIS MSIL multiple catch blocks no primary key Nullable Osmos Osmotic Osmotic communication Osmotic communications page events page life cycle partial class PMI powerapps preserve precision points private contructor ProcessExit Project management properties property protect connectionString providerName providers query regular expression repository Responsive Web Design return type run-time RWD Saas self join session session expiry sessions singelton singleton pattern software as a service source control system SQLMetal string time management time-boxing toolstrip ToolStrip controls ToolStripControlHost tortoise SVN ToString() try catch finally update wcf web application web design web site web.config where-clause xml

Pages