Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8046821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:54:52+00:00 2026-06-05T05:54:52+00:00

I am writing a little program that iterates through all files in a directory

  • 0

I am writing a little program that iterates through all files in a directory and searches for a substring.
It’s basically something like this:

s = File.ReadAllText(FileName)
If s.Contains("Find this substring") Then
    MatchesFound += 1
End If

I also have a Regex version of this program, but still using File.ReadAllText() to read the files.

Should I be concerned with calling File.ReadAllText() on binary files?
I don’t mind getting a few false positives in the search results, but I don’t want my program to crash.
MSDN docs don’t show any exceptions for this method that result from not being able to read or interpret file data.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T05:54:54+00:00Added an answer on June 5, 2026 at 5:54 am

    Your program won’t crash. If the file is too long, it might just take up lot of memory. ReadAllText releases file handle before returning to you. As such, your handles would get properly disposed.

    Your string will just have text representation of the binary file. Most of it probably would be invalid characters. Framework internally uses unicode for string (UTF16).

    Only thing you should be concerned about is extremely large files, e.g. a 4GB ISO file. If you have files that big in your directory then you should probably make better algorithm to make code efficient instead of blindly getting ReadAllText.

    Also, before you read, you can check file size; and if its obvious that its a pure binary file (for ex. 100MB zip file); you can skip that and move to next.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing this little python program that will get new posts on the
I 'm writing a little program that implements pipes like they work in the
Im writing a little python program that goes through an XML file and does
I've been writing a little program for fun that transfers files over TCP in
I'm writing a little program that basically has a bunch of buttons that when
I'm writing a simple little ocaml program that reads an algebraic statement in from
When I start GVim and start writing my little program I'd like to save
I am writing a little program in C# that scans a folder and opens
I was writing a little program that takes a list and generates a menu
I'm writing a simple little program in VB.NET that has to play two sounds

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.