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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:29:49+00:00 2026-05-29T06:29:49+00:00

I am using fs.Length , where fs is a FileStream . Is this an

  • 0

I am using fs.Length, where fs is a FileStream.

Is this an O(1) operation? I would think this would just read from the properties of the file, as opposed to going through the file to find when the seek position has reached the end. The file I am trying to find the length of could easily range from 1 MB to 4-5 GB.

However I noticed that there is a FileInfo class, which also has a Length property.

Do both of these Length properties theoretically take the same amount of time? Or does is fs.Length slower because it must open the FileStream first?

  • 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-05-29T06:29:50+00:00Added an answer on May 29, 2026 at 6:29 am

    The natural way to get the file size in .NET is the FileInfo.Length property you mentioned.

    I am not sure Stream.Length is slower (it won’t read the whole file anyway), but it’s definitely more natural to use FileInfo instead of a FileStream if you do not plan to read the file.


    Here’s a small benchmark that will provide some numeric values:

    private static void Main(string[] args)
    {
        string filePath = ...;   // Path to 2.5 GB file here
    
        Stopwatch z1 = new Stopwatch();
        Stopwatch z2 = new Stopwatch();
    
        int count = 10000;
    
        z1.Start();
        for (int i = 0; i < count; i++)
        {
            long length;
            using (Stream stream = new FileStream(filePath, FileMode.Open))
            {
                length = stream.Length;
            }
        }
    
        z1.Stop();
    
        z2.Start();
        for (int i = 0; i < count; i++)
        {
            long length = new FileInfo(filePath).Length;
        }
    
        z2.Stop();
    
        Console.WriteLine(string.Format("Stream: {0}", z1.ElapsedMilliseconds));
        Console.WriteLine(string.Format("FileInfo: {0}", z2.ElapsedMilliseconds));
    
        Console.ReadKey();
    }
    

    Results:

    Stream: 886
    FileInfo: 727
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using this for sending file to user header('Content-type: application/zip'); header('Content-Length: ' .
I'm saving an uploaded image using this code: using (var fileStream = File.Create(savePath)) {
I'm using valums ajax file-uploader My nodejs server side looks like this: fileStream =
How would I get the length of an ArrayList using a JSF EL expression?
I'm using fgets with stdin to read in some data, with the max length
While googling, I see that using java.io.File#length() can be slow. FileChannel has a size()
I'm using FileStream to write to a file, and watching the underlying system calls
I'm overwriting a file using C# in Windows Phone 7. When I do this
My program needs to read chunks from a huge binary file with random access.
I am using the following code to attempt to read a large file (280Mb)

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.