An extension method for converting a byte array to a string (reading its BOM)
Below is an extension method that adapts Rick Strahl’s code (and the comments too) to stop you having to guess or read the byte order mark of a byte array or text file each time you convert it to a string.
The commonest way of doing this is usually with
byte buffer = File.ReadAllBytes(@"C:\file.txt"); System.Text.Encoding.xxx.GetString(buffer)
where xxx is Ascii, Unicode, or UTF8, or Default, but the extension method you to simply do
byte buffer = File.ReadAllBytes(@"C:\file.txt"); string content = buffer.GetString();
Here’s the method