Encoding.GetEncoding Method (String)

 

Returns the encoding associated with the specified code page name.

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

static member GetEncoding : 
        name:string -> 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.

Return Value

Type: System.Text.Encoding

The encoding associated with the specified code page.

Exception Condition
ArgumentException

name is not a valid code page name.

-or-

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

The fallback handler depends on the encoding type of name. If name is a code page or double-byte character set (DBCS) encoding, a best-fit fallback handler is used. Otherwise, a replacement fallback handler is used. These fallback handlers may not be appropriate for your app. To specify the fallback handler used by the encoding specified by name, you can call the GetEncoding(String, EncoderFallback, DecoderFallback) overload.

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(String) method returns any additional encodings that are made available by registering an EncodingProvider object.

System_CAPS_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 in 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 gets two instances of the same encoding (one by code page and another by name), and checks their equality.

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

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: