Remove Byte Order Mark from a File.ReadAllBytes (byte[])

JC Grubbs picture JC Grubbs · Nov 13, 2008 · Viewed 15.5k times · Source

I have an HTTPHandler that is reading in a set of CSS files and combining them and then GZipping them. However, some of the CSS files contain a Byte Order Mark (due to a bug in TFS 2005 auto merge) and in FireFox the BOM is being read as part of the actual content so it's screwing up my class names etc. How can I strip out the BOM characters? Is there an easy way to do this without manually going through the byte array looking for ""?

Answer

JaredPar picture JaredPar · Nov 14, 2008

Expanding on Jon's comment with a sample.

var name = GetFileName();
var bytes = System.IO.File.ReadAllBytes(name);
System.IO.File.WriteAllBytes(name, bytes.Skip(3).ToArray());