Get-Content source codepage?

自作多情 提交于 2019-12-25 03:42:48

问题


I have a plain text .txt file encoded with Windows codepage 1250. I need to read the file on an English/Western Europe Windows system which uses codepage 1252.

The Get-Content -Encoding parameter expects a text string from a fixed set which does not include other codepages.

I can use GetEncoding(), but this result cannot be used as the -Encoding parameter to Get-Content.

How can I tell Get-Content to read the file using 1250 encoding?

PS C:\src\t> [System.Text.Encoding]::GetEncoding('windows-1250')


IsSingleByte      : True
BodyName          : iso-8859-2
EncodingName      : Central European (Windows)
HeaderName        : windows-1250
WebName           : windows-1250
WindowsCodePage   : 1250
IsBrowserDisplay  : True
IsBrowserSave     : True
IsMailNewsDisplay : True
IsMailNewsSave    : True
EncoderFallback   : System.Text.InternalEncoderBestFitFallback
DecoderFallback   : System.Text.InternalDecoderBestFitFallback
IsReadOnly        : True
CodePage          : 1250

回答1:


How can I tell Get-Content to read the file using 1250 encoding?

I'm afraid you can't. Get-Content expects to be given one of the FileSystemCmdletProviderEncoding enum values, and they are not fine-grained enough. But you can easily use the native .NET facilities to read the file.

$windows1250 = [System.Text.Encoding]::GetEncoding('windows-1250')
$path = ".\path\to\your\file.txt"
$text = [System.IO.File]::ReadAllText($path, $windows1250)


来源:https://stackoverflow.com/questions/53973986/get-content-source-codepage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!