Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

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
)
public static Encoding GetEncoding (
	String name
)
public static function GetEncoding (
	name : String
) : Encoding

Parameters

name

The code page name of the preferred encoding.

Return Value

The Encoding associated with the specified code page.

Exception typeCondition

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.

For a list of code pages, see the Encoding class topic. Or use the GetEncodings method to get a list of all encodings.

To get the encoding associated with the default ANSI code page in the system's regional settings, use GetEncoding(0) or the Default property. To determine the default code pages used on the system, use the Windows API GetSystemDefaultLangID. To determine the current ANSI code page, use the Windows API GetACP.

GetEncoding returns a cached instance with default settings. Use the constructors of derived classes to get an instance with different settings; for example, the UTF32Encoding class provides a constructor that allows you to enable error detection.

The following code 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( 65005 );

      // 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

*/


import System.*;
import System.Text.*;

public class SamplesEncoding
{
    public static void main(String[] args)
    {
        // Get a UTF-32 encoding by codepage.
        Encoding e1 = Encoding.GetEncoding(65005);

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

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

/* 
This code produces the following output.

e1 equals e2? True

*/


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft