Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
GetPreamble Method

Encoding.GetPreamble Method

Returns a set of bytes used at the beginning of a stream to determine which encoding a file was created with. This can include the Unicode byte order mark.

[Visual Basic]
Public Overridable Function GetPreamble() As Byte()
[C#]
public virtual byte[] GetPreamble();
[C++]
public: virtual unsigned char GetPreamble()  __gc[];
[JScript]
public function GetPreamble() : Byte[];

Return Value

The bytes at the beginning of a stream, which typically comprise the byte order mark.

Remarks

The preamble can be the Unicode byte order mark (U+FEFF written in the appropriate encoding) or any other type of identifying marks. This method can return an empty byte array.

Example

[Visual Basic] 
Imports System
Imports System.Text

Namespace GetPreambleExample
   Class GetPreambleExampleClass
      Shared Sub Main()
         Dim [unicode] As Encoding = Encoding.Unicode

         ' Get the preamble for the Unicode encoder. 
         ' In this case the preamble contains the byte order mark (BOM).
         Dim preamble As Byte() = [unicode].GetPreamble()

         ' Make sure a preamble was returned 
         ' and is large enough to contain a BOM.
         If preamble.Length >= 2 Then
            If preamble(0) = &HFE And preamble(1) = &HFF Then
               Console.WriteLine("The Unicode encoder is encoding in big-endian order.")
            Else
               If preamble(0) = &HFF And preamble(1) = &HFE Then
                  Console.WriteLine("The Unicode encoder is encoding in little-endian order.")
               End If
            End If
         End If
      End Sub
   End Class
End Namespace

[C#] 
using System;
using System.Text;

namespace GetPreambleExample
{
   class GetPreambleExampleClass
   {
      static void Main()
      {
         Encoding unicode = Encoding.Unicode;

         // Get the preamble for the Unicode encoder. 
         // In this case the preamble contains the byte order mark (BOM).
         byte[] preamble = unicode.GetPreamble();

         // Make sure a preamble was returned 
         // and is large enough to containa BOM.
         if(preamble.Length >= 2)
         {
            if(preamble[0] == 0xFE && preamble[1] == 0xFF)
            {
               Console.WriteLine("The Unicode encoder is encoding in big-endian order.");
            }
            else if(preamble[0] == 0xFF && preamble[1] == 0xFE)
            {
               Console.WriteLine("The Unicode encoder is encoding in little-endian order.");
            }
         }
      }
   }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Text;

int main() 
{
   Encoding * unicode = Encoding::Unicode;

   // Get the preamble for the Unicode encoder. 
   // In this case the preamblecontains the Byte order mark (BOM).
   Byte preamble[] = unicode -> GetPreamble();

   // Make sure a preamble was returned 
   // and is large enough to containa BOM.
   if (preamble -> Length >= 2) 
   {
      // if (preamble->Item[0] == 0xFE && preamble->Item[1] == 0xFF) 
      if (preamble[0] == 0xFE && preamble[1] == 0xFF) 
      {
         Console::WriteLine(S"The Unicode encoder is encoding in big-endian order.");
      } 
      // else if (preamble->Item[0] == 0xFF && preamble->Item[1] == 0xFE) 
      else if (preamble[0] == 0xFF && preamble[1] == 0xFE) 
      {
         Console::WriteLine(S"The Unicode encoder is encoding in little-endian order.");
      }
   }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Encoding Class | Encoding Members | System.Text Namespace

Show:
© 2015 Microsoft