System.Text.Encoding.GetEncoding Method

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

Syntax

public static Encoding GetEncoding (int codepage, EncoderFallback encoderFallback, DecoderFallback decoderFallback)

Parameters

codepage
The code page identifier of the preferred encoding. Possible values are listed in the Code Page column of the table that appears in the System.Text.Encoding class topic.
encoderFallback
An object that provides an error-handling procedure when a character cannot be encoded with the current encoding.
decoderFallback
An object that provides an error-handling procedure when a byte sequence cannot be decoded with the current encoding.

Returns

The encoding that is associated with the specified code page.

Remarks

Note:

Some unsupported code pages cause the exception ArgumentException to be thrown, whereas others cause NotSupportedException. Therefore, your code must catch all exceptions indicated in the Exceptions section.

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

Note:

The ANSI code pages can be different on different computers, or can be changed for a single computer, leading to data corruption. For this reason, encoding and decoding data using the default code page returned by Encoding.GetEncoding(0) is not recommended. For the most consistent results, applications 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 System.Text.Encoding class topic. The application uses the Encoding.GetEncodings method to get a list of all encodings.

To get the encoding associated with the default ANSI code page in the operating system's regional and language settings, the application can either use a setting of 0 for the codepage parameter or use the Encoding.Default property. To determine the default code pages used on the system, use the Windows GetSystemDefaultLangID function. To determine the current ANSI code page, use the Windows GetACP function.

Encoding.GetEncoding(int) returns a cached instance with default settings. The application should use the constructors of derived classes to get an instance with different settings. For example, the System.Text.UTF32Encoding class provides a constructor that lets you enable error detection.

Requirements

Namespace: System.Text
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0
Since: .NET 2.0