Alex Rivera | Logout

How can I determine if a file is binary or text in c#?

Asked 2009-05-26T14:05:20.297
61

I need to determine in 80% if a file is binary or text, is there any way to do it even quick and dirty/ugly in c#?

Edit
Report

2 Answers

36

There's a method called Markov Chains. Scan a few model files of both kinds and for each byte value from 0 to 255 gather stats (basically probability) of a subsequent value. This will give you a 64Kb (256x256) profile you can compare your runtime files against (within a % threshold).

Supposedly, this is how browsers' Auto-Detect Encoding feature works.

answered 2009-05-26T14:16:52.567
2

A really really really dirty way would be to build a regex that takes only standard text, punctuation, symbols, and whitespace characters, load up a portion of the file in a text stream, then run it against the regex. Depending on what qualifies as a pure text file in your problem domain, no successful matches would indicate a binary file.

To account for unicode, make sure to mark the encoding on your stream as such.

This is really suboptimal, but you said quick and dirty.

answered 2009-05-26T14:24:16.827

Your Answer