Detect and Prevent Binary File from Opening

SyntaxEditor for Windows Forms Forum

Posted 14 years ago by Boyd - Automated Software Testing Consultant / Developer, Patterson Consulting, LLC
Avatar
I'm trying to prevent my users from opening a binary file in SyntaxEditor, but I'm at a loss for how to actually detect that a file is binary before loading it in SyntaxEditor. I'm sure this is something simple, so can someone please help me out? I appreciate the assistance.

Comments (3)

Posted 14 years ago by Boyd - Automated Software Testing Consultant / Developer, Patterson Consulting, LLC
Avatar
I still haven't figured this one out... anyone have any ideas?
Posted 14 years ago by Marianne
Avatar
What you might try is getting the first 10 or 100 or 500 bytes of the file and examining each byte to see if its in the range of alphanumeric characters or special text chars like CR, LF, TAB, etc. If X percent of the chars are not in the range of what would normally be included in a text file, then it probably isn't a text file.

The only thing that would make this difficult is the encoding, whether its Unicode or UTF-8, etc., for unicode you'd have to examine two bytes at a time, etc. So, in the end, it may be a bit more challenging than you had hoped for, but I certainly think it's doable.

------------------------------- Marianne

Posted 14 years ago by Boyd - Automated Software Testing Consultant / Developer, Patterson Consulting, LLC
Avatar
Marianne,

Thanks for the suggestion. It sounds like there's no easy way to do this, so your approach sounds good. I'll have to give this a try. I appreciate the feedback!
The latest build of this product (v2018.1 build 0341) was released 3 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.