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 386357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:34:08+00:00 2026-05-12T15:34:08+00:00

I came across this piece of code today: public static byte[] ReadContentFromFile(String filePath) {

  • 0

I came across this piece of code today:

public static byte[] ReadContentFromFile(String filePath)
{
    FileInfo fi = new FileInfo(filePath);
    long numBytes = fi.Length;
    byte[] buffer = null;
    if (numBytes > 0)
    {
        try
        {
            FileStream fs = new FileStream(filePath, FileMode.Open);
            BinaryReader br = new BinaryReader(fs);
            buffer = br.ReadBytes((int)numBytes);
            br.Close();
            fs.Close();
        }
        catch (Exception e)
        {
            System.Console.WriteLine(e.StackTrace);
        }
    }
    return buffer;
}

My first thought is to refactor it down to this:

public static byte[] ReadContentFromFile(String filePath)
{
    return File.ReadAllBytes(filePath);
}

System.IO.File.ReadAllBytes is documented as:

Opens a binary file, reads the
contents of the file into a byte
array, and then closes the file.

… but am I missing some key difference?

  • 1 1 Answer
  • 1 View
  • 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-05-12T15:34:08+00:00Added an answer on May 12, 2026 at 3:34 pm

    The original code returns a null reference if the file is empty, and won’t throw an exception if it can’t be read. Personally I think it’s better to return an empty array, and to not swallow exceptions, but that’s the difference between refactoring and redesigning I guess.

    Oh, also, if the file length is changed between finding out the length and reading it, then the original code will read the original length. Again, I think the File.ReadAllBytes behaviour is better.

    What do you want to happen if the file doesn’t exist?

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

Sidebar

Related Questions

I came across this piece of code today: File.SetAttributes(excelFileName, File.GetAttributes(excelFileName) & ~ (FileAttributes.Archive |
I was looking at the YUI Compressor and came across this piece of code
I came across this piece of batch code. It should find the path to
I came across this piece of ruby code: str[-1]==?? What is the double question
I came across this problem when referring a piece of JavaScript code: <a href="javascript:void(0);"><!--
What does the +\ operator do in Python? I came across this piece of
Below is my code to transform XML using XSLT: Private Sub Transform(ByVal XslUri As
I am working on a windows form that has a TabControl named tabDocuments. I
I got assigned to work on some performance and random crashing issues of a

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.