Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

System.Text.Encoding.GetEncodings() does not show all available encodings after call RegisterProvider()

Problem:

If you want to use character encodings other than the default registered encodings, you have to call [System.Text.Encoding]::RegisterProvider([System.Text.CodePagesEncodingProvider]::Instance). But even if you call that method, the output of [System.Text.Encoding]::GetEncodings() will not be changed.

PS > [System.Text.Encoding]::RegisterProvider([System.Text.CodePagesEncodingProvider]::Instance)
PS > [System.Text.Encoding]::GetEncodings()
CodePage Name       DisplayName
-------- ----       -----------
    1200 utf-16     Unicode
    1201 utf-16BE   Unicode (Big-Endian)
   12000 utf-32     Unicode (UTF-32)
   12001 utf-32BE   Unicode (UTF-32 Big-Endian)
   20127 us-ascii   US-ASCII
   28591 iso-8859-1 Western European (ISO)
   65000 utf-7      Unicode (UTF-7)
   65001 utf-8      Unicode (UTF-8)

Reason:

The behavior is not documented in by design. The API document ( https://msdn.microsoft.com/ja-jp/library/system.text.encoding.getencodings(v=vs.110).aspx ) says:

The list of supported encodings returned by the GetEncodings method does not include any additional encodings made available by any EncodingProvider implementations that were registered by calls to the RegisterProvider method.

Solution:

You can get the actual available encodings like this ( from http://stknohg.hatenablog.jp/entry/2016/08/22/231538 ):

for ($i = 0; $i -lt 65535; $i++){
    try{
        $enc = [System.Text.Encoding]::GetEncoding($i)
        Write-Output ("{0}, {1}, {2}" -f $i, $enc.WebName, $enc.EncodingName)
    }
    catch{}
}