Closed Thread
Results 1 to 6 of 6

Thread: Determine if a file is binary

  1. #1
    Nightracer's Avatar
    Nightracer is offline Programmer
    Join Date
    Jun 2006
    Posts
    131
    Rep Power
    0

    Determine if a file is binary

    I'm writing a function that goes through all files in a directory and reads all files that are text. I ran into a problem in that it begins to read binary files as well. How can I determine whether a file is binary or text in C#?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Aug 2006
    Posts
    11,209
    Blog Entries
    6
    Rep Power
    101
    Well perhaps you could try setting the extensions, so it scans for example only the files with the extensions of .txt, .text, .doc, etc.....

  4. #3
    Void's Avatar
    Void is offline Programming Expert
    Join Date
    Jun 2006
    Posts
    410
    Rep Power
    23
    I'd assume that you would have to scan extensions unless you are in a unix system. Not quite sure how you would do it then.
    Void

  5. #4
    brackett is offline Programmer
    Join Date
    May 2006
    Posts
    192
    Rep Power
    22
    Since text is binary, it's not an easy question. Usually, Windows apps use extensions to know what the type of a file is. Alternatively, you could sniff the file contents - if it contains all lower bytes (< 127), I think it'd be safe to assume it's ASCII text. Unicode would be a bit harder, though you could check for a BOM (Byte Order Mark) at the beginning perhaps.

  6. #5
    Nightracer's Avatar
    Nightracer is offline Programmer
    Join Date
    Jun 2006
    Posts
    131
    Rep Power
    0
    So I would have to read every file to determine if it were binary or ascii? That seems like a lot of processing power. I'll have to think of something else.

  7. #6
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143
    If you just read the first line of the file, you should be able to analyze that. You could also download the Windows version of the Unix command line tools and use grep " " *. It will return which files are binary.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Binary file modification
    By eafkuor in forum C and C++
    Replies: 7
    Last Post: 04-18-2011, 09:29 AM
  2. copying binary file in C
    By nerio in forum C and C++
    Replies: 4
    Last Post: 11-27-2010, 05:06 AM
  3. ASP: Saving binary to file
    By s0mmer in forum ASP, ASP.NET and Coldfusion
    Replies: 9
    Last Post: 01-23-2010, 03:10 AM
  4. Determine if File has Changed
    By oppo in forum General Programming
    Replies: 5
    Last Post: 10-31-2007, 01:36 AM
  5. Writing to binary file
    By paul. in forum Pascal and Delphi
    Replies: 4
    Last Post: 04-13-2007, 06:23 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts