.NET Framework Class Library
Encoding..::.GetEncoding Method (String)

Returns an encoding associated with the specified code page name.

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

Visual Basic (Declaration)
Public Shared Function GetEncoding ( _
    name As String _
) As Encoding
Visual Basic (Usage)
Dim name As String
Dim returnValue As Encoding

returnValue = Encoding.GetEncoding(name)
C#
public static Encoding GetEncoding(
    string name
)
Visual C++
public:
static Encoding^ GetEncoding(
    String^ name
)
JScript
public static function GetEncoding(
    name : String
) : Encoding

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.
Exceptions

ExceptionCondition
ArgumentException

name is not a valid code page name.

-or-

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

Remarks

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.

Examples

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

Visual Basic
Imports System
Imports System.Text

Public Class SamplesEncoding   

   Public Shared Sub Main()

      ' Get a UTF-32 encoding by codepage.
      Dim e1 As Encoding = Encoding.GetEncoding(12000)

      ' Get a UTF-32 encoding by name.
      Dim e2 As Encoding = Encoding.GetEncoding("utf-32")

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

   End Sub 'Main 

End Class 'SamplesEncoding


'This code produces the following output.
'
'e1 equals e2? True

C#
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

*/

Visual C++
using namespace System;
using namespace System::Text;
int 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

*/
Platforms

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.
Version Information

.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
See Also

Reference

Other Resources

Tags :


Community Content

dmatson
Warning: This overload has security issues
This overload has issues similar to those noted on http://msdn.microsoft.com/en-us/library/system.text.unicodeencoding.aspx.

To enable error detection and to make the class instance more secure, the application should use the GetEncoding overload that takes an EncoderFallback and a DecoderFallback to detect errors. Without error detection, no exception is thrown, and the invalid sequence is generally ignored.
Tags : contentbug

Page view tracker