Windows: Determine Textencoding of a File

Hi,

if you are working with special characters (i.e. German Umlaute) within a Textfile it is importent to know with which text encoding (UTF8, ASCII…) a file is saved.

This cannot be be determine when a file is opened in Textmode because each file is converted (.NET) to UTF16 encoding into memory.

The solution is to open the file as stream, and read it. Here a powershell solution:

PS D:\> $oFileStream=New-Object System.IO.StreamReader("D:\myTextFile.ps1",$true)
PS D:\> $oFileStream.Read()
PS D:\> $oFileStream.CurrentEncoding
BodyName          : utf-8
EncodingName      : Unicode (UTF-8)
HeaderName        : utf-8
WebName           : utf-8
WindowsCodePage   : 1200
IsBrowserDisplay  : True
IsBrowserSave     : True
IsMailNewsDisplay : True
IsMailNewsSave    : True
IsSingleByte      : False
EncoderFallback   : System.Text.EncoderReplacementFallback
DecoderFallback   : System.Text.DecoderReplacementFallback
IsReadOnly        : True
CodePage          : 65001
PS D:\> $oFileStream.Close()

Michael

Leave a Reply Cancel reply