问题
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