This documentation is archived and is not being maintained.

Encoding.GetEncoding Method (String)

Returns an encoding associated with the specified code page name.

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

public static Encoding GetEncoding(
	string name
)

Parameters

name
Type: System.String

The code page name of the preferred encoding. Any value returned by WebName is a valid input.

Return Value

Type: System.Text.Encoding
The Encoding 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.

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, 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 Encoding class topic. The application uses the 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 use a setting of 0 for the codepage parameter or the Default property. To determine the default code pages used on the system, the application uses the Windows API function GetSystemDefaultLangID. To determine the current ANSI code page, the application uses the Windows API function GetACP.

GetEncoding 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 UTF32Encoding class provides a constructor that allows enabling of error detection.

The following example gets two instances of the same encoding (one by codepage and another by name), and checks their equality.

using System;
using System.Text;

public class SamplesEncoding  {

   public static void Main()  {

      // Get a UTF-32 encoding by codepage.
      Encoding e1 = Encoding.GetEncoding( 12000 );

      // Get a UTF-32 encoding by name.
      Encoding e2 = Encoding.GetEncoding( "utf-32" );

      // Check their equality.
      Console.WriteLine( "e1 equals e2? {0}", e1.Equals( e2 ) );

   }

}


/* 
This code produces the following output.

e1 equals e2? True

*/

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Show: