C# – Check if File is Text Based

In general: there is no way to tell.

A text file stored in UTF-16 will likely look like binary if you open it with an 8-bit encoding. Equally someone could save a text file as a .doc (it is a document).

While you could open the file and look at some of the content all such heuristics will sometimes fail (eg. notepad tries to do this, by careful selection of a few characters notepad will guess wrong and display completely different content).

If you have a specific scenario, rather than being able to open and process anything, you should be able to do much better.

Leave a Comment