byte[] byteArray = File.ReadAllBytes(filePath);
// count every second byte array if its zero.
int zeroBytesCount = 0;
for (int i = 1; i < byteArray.Length; i += 2)
{
if (byteArray[i] == 0)
{
zeroBytesCount++;
}
}
Encoding encoding = Encoding.UTF8;
// if count is bigger or equal to 40% of the byte array, it most likely UTF16
if (zeroBytesCount >= byteArray.Length * 0.4)
{
encoding = Encoding.Unicode;
}
else
{
DetectionDetail detector = CharsetDetector.DetectFromBytes(byteArray).Detected;
encoding = detector.Encoding;
}
UTF16_Test.csv
For the provided file the encoding is not detected as
UTF16 LE
even though it is.As workaround i am now using this code