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
Not applicable.

Parameters

name

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

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.

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 GetEncoding(0) 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 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 Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft