Export (0) Print
Expand All

Encoding.GetEncoding Method (String, EncoderFallback, DecoderFallback)

Returns the encoding associated with the specified code page name. Parameters specify an error handler for characters that cannot be encoded and byte sequences that cannot be decoded.

Namespace:  System.Text
Assembly:  mscorlib (in mscorlib.dll)

static member GetEncoding : 
        name:string * 
        encoderFallback:EncoderFallback * 
        decoderFallback:DecoderFallback -> Encoding

Parameters

name
Type: System.String

The code page name of the preferred encoding. Any value returned by the WebName property is valid. Possible values are listed in the Name column of the table that appears in the Encoding class topic.

encoderFallback
Type: System.Text.EncoderFallback

An object that provides an error-handling procedure when a character cannot be encoded with the current encoding.

decoderFallback
Type: System.Text.DecoderFallback

An object that provides an error-handling procedure when a byte sequence cannot be decoded with the current encoding.

Return Value

Type: System.Text.Encoding
The encoding that is associated with the specified code page.

ExceptionCondition
ArgumentException

name is not a valid code page name.

-or-

The code page indicated by name is not supported by the underlying platform.

The GetEncoding method relies on the underlying platform to support most code pages. However, the .NET Framework natively supports some encodings.

In addition to the encodings that are intrinsically supported on a specific platform version of the .NET Framework, the GetEncoding(Int32) method returns any additional encodings that are made available by registering an EncodingProvider object.

NoteNote

The ANSI code pages can be different on different computers, or can be changed for a single computer, leading to data corruption. For the most consistent results, you should use Unicode, such as UTF-8 (code page 65001) or UTF-16, instead of a specific code page.

For a list of code pages, see the Encoding class topic. You can call the GetEncodings method on the full .NET Framework on the Windows desktop to get a list of all encodings.

GetEncoding returns a cached instance with default settings. You should use the constructors of derived classes to get an instance with different settings. For example, the UTF32Encoding class provides a constructor that lets you enable error detection.

The following example demonstrates the Encoding.GetEncoding(String, EncoderFallback, DecoderFallback) method.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft