Check FIle Encoding Programmatically In-C#

In this article, I will show how to detect text file in UTF8 or ASCII character. As you know ASCII support 127 characters and its only support English character.
If the user wrongly saves the file, then it will show you some garbage character. In this article, I will show you how to verify the text file is UTF-8 Encoding or not.

Consider the following example. User type the word in Hindi and save the file as ASCII 

$ads={1}

When the user or system read the file, it found a garbage character because the file does not have BOM information saved.

I will show you how to detect the file programmatically is saved as UTF8 or not.

void Main()
{
	var bytes = File.ReadAllBytes(@"c:\temp\UTF8.txt");

	var third = bytes.Take(3);
	var encoding = IsUTF8Encodig(third.ToArray());
	Console.WriteLine(encoding);


}

public bool IsUTF8Encodig(byte[] data)
{
	var third = data.Take(3);
	return BitConverter.ToString(third.ToArray()) == "EF-BB-BF";
}
Next Post Previous Post
No Comment
Add Comment
comment url