Delphi – TStringList.LoadFromFile Unicode


I am attempting to open a txt file to a StringList but if I open a UTF-8 format it fails to load, this is confusing because I have Unicode XE2, am I missing something stupid here?

Simple Sample

Sl := tStringList.Create;


For i =0 to SL.Count -1 do

but the String does not load when the txt file is UTF-8 but works fine when its in ANSI format.

Best Solution

TStringList.LoadFromFile will attempt to infer the encoding from the file's byte order mark (BOM). If no BOM is present then ANSI encoding is assumed.

In your case it seems clear that there is no BOM, so you must tell LoadFromFile which encoding to use. Do that by specifying the encoding as the second argument passed to LoadFromFile:

SL.LoadFromFile(sFilePath, TEncoding.UTF8);
Related Question