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

  • SEARCH
  • Home
  • 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 567461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:05:07+00:00 2026-05-13T13:05:07+00:00

I wrote the following method to determine the max file size: public static long

  • 0

I wrote the following method to determine the max file size:

    public static long GetMaxFileSize(string dirPath, long maxFileSize)
    {
        DirectoryInfo [] dirInfos = new DirectoryInfo(dirPath).GetDirectories();
        foreach (DirectoryInfo dirInfo in dirInfos)
        {
            DirectoryInfo [] subDirInfos = dirInfo.GetDirectories();
            foreach (DirectoryInfo subDirInfo in subDirInfos)
                maxFileSize = GetMaxFileSize(dirInfo.FullName, maxFileSize);

            FileInfo [] fileInfos = dirInfo.GetFiles();
            foreach (FileInfo fileInfo in fileInfos)
            {
                if (maxFileSize < fileInfo.Length)
                    maxFileSize = fileInfo.Length;
            }
        }

        return maxFileSize;
    }

Code Complete recommends to “use recursion selectively”. That being the case, I was wondering if the community thought this was a valid use of recursion. If not, is there a better technique of doing this?

EDIT: I can’t use LINQ because its not available in .NET 2.0, but I don’t want to tag this as a .NET 2.0 question only to further discussion points like Jared’s below.

EDIT: Cleaned up code based on an issue that was spotted in not getting the root directory’s files.

   public static long GetMaxFileSize(DirectoryInfo dirInfo, long maxFileSize)
   {
       DirectoryInfo [] subDirInfos = dirInfo.GetDirectories();
       foreach (DirectoryInfo subDirInfo in subDirInfos)
       {
           maxFileSize = GetMaxFileSize(subDirInfo, maxFileSize);
       }

       FileInfo [] fileInfos = dirInfo.GetFiles();
       foreach (FileInfo fileInfo in fileInfos)
       {
           if (maxFileSize < fileInfo.Length)
               maxFileSize = fileInfo.Length;
       }

       return maxFileSize;
   }
  • 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-13T13:05:07+00:00Added an answer on May 13, 2026 at 1:05 pm

    I think a better way is to the File System API to do the searching for you via Directory.GetFiles. This method provides automatic searching of sub-directories. This eliminates the question of whether or not to recurse and instead leaves the decision of how to implement it on the designer of the API (who likely designed it for such a scenario).

    This method combined with LINQ provides a very elegant solution

    var max = Directory
      .GetFiles(path, "*", SearchOption.AllDirectories)
      .Select(x => new FileInfo(x))
      .Select(x => x.Length)
      .Max();
    

    EDIT As Jimmy pointed out, for 4.0 and higher, it’s better to use EnumerateFiles to avoid the overhead of creating a potentially large array

    var max = Directory
      .EnumerateFiles(path, "*", SearchOption.AllDirectories)
      .Select(x => new FileInfo(x))
      .Select(x => x.Length)
      .Max();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day all I wrote the following method: private void RegisterEvent(object targetObject, string eventName,
Using an idea from Bob King idea I wrote the following method. It works
I just wrote the following C++ function to programmatically determine how much RAM a
I needed some simple string encryption, so I wrote the following code (with a
I wrote a setter and getter method following Apple's conventions and noticed that despite
I tried the following code: static void PostTweetanother(string username, string password, string tweet) {
I have the following method that I wrote for Project Euler - Problem 36
HI, I have the following code: class Libr { public: Libr(); std::string book; class
I wrote the following code for finding square root by Newton's method by successive
I wrote the following code: public class Point2 { private double _radius , _alpha;

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.