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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:41:06+00:00 2026-06-11T14:41:06+00:00

I am creating a program that cursively finds all the files and directories in

  • 0

I am creating a program that cursively finds all the files and directories in the specified path. So one node may have other nodes if that node happens to be a directory.

Here is my Node class:

class Node
{
    public List<Node> Children = new List<Node>(); // if node is directory then children will be the files and directories in this direcotry
    public FileSystemInfo Value { get; set; }  // can eather be a FileInfo or DirectoryInfo

    public bool IsDirectory 
    { 
        get{ return Value is DirectoryInfo;} 
    }

    public long Size   // HERE IS WHERE I AM HAVING PROBLEMS! I NEED TO RETRIEVE THE 
    {                  // SIZE OF DIRECTORIES AS WELL AS FOR FILES.
        get
        {
            long sum = 0;

            if (Value is FileInfo)
                sum += ((FileInfo)Value).Length;
            else
                sum += Children.Sum(x => x.Size);

            return sum;
        }
    }

    // this is the method I use to filter results in the tree
    public Node Search(Func<Node, bool> predicate)
    {
         // if node is a leaf
         if(this.Children.Count==0)
         {
             if (predicate(this))
                return this;
             else
                return null;
         }
         else // Otherwise if node is not a leaf
         {
             var results = Children.Select(i => i.Search(predicate)).Where(i => i != null).ToList();

             if (results.Any()) // THIS IS HOW REMOVE AND RECUNSTRUCT THE TREE WITH A FILTER
             {
                var result = (Node)MemberwiseClone();
                result.Children = results;
                return result;
             }
             return null;
         }             
    }

}

and thanks to that node class I am able to display the tree as:

enter image description here

In one column I display the name of the directory or file and on the right the size. The size is formated as currency just because the commas help visualize it more clearly.

So now my problem is The reason why I have this program was to perform some advance searches. So I may only want to search for files that have the “.txt” extension for example. If I perform that filter on my tree I will get:

enter image description here

(note that I compile the text to a function that takes a Node and returns a bool and I pass that method to the Search method on my Node class in order to filter results. More information on how to dynamically compile code can be found at: http://www.codeproject.com/Articles/10324/Compiling-code-during-runtime) Anyways that has nothing to do with this question. The important part was that I removed all the nodes that did not matched that criteria and because I removed those nodes now the sizes of the directories changed!!!

So my question is how will I be able to filter results maintaining the real size of the directory. I guess I will have to remove the property Size and replace it with a field. The problem with that is that every time I add to the tree I will have to update the size of all the parent directories and that gets complex. Before starting coding it that way I will appreciate your opinion on how I should start implementing the class.

  • 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-11T14:41:08+00:00Added an answer on June 11, 2026 at 2:41 pm

    Since you’re using recursion and your weight is a node-level property you can’t expect that will continue to sum even after you remove the node. You either promote it to a upper level (collection) or use an external counter within the recursion (which counts but not depending on filter, you’ll need to carry this through the recuersion).

    Anyway, why are you implementing a core .NET functionality again? any reason beyond filtering or recursive search? both are pretty well implemented in the BCL.

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

Sidebar

Related Questions

im creating a program that will compile java files, at the moment i have
I am creating a program that uses JFrame, JPanel, JLabel and all other sorts
When creating DLL files for a program that already exists, is it customary to
Im creating a program that is supposed have the user enter a student name
I am creating a program that among other things is going to control Services.
I am creating a program that encrypts/hashs messages I have a list that contains
I'm creating a program that installs a keyboard hook to capture all keys and
I'm planning on creating a program that will have a login form and then
I have been tasked with creating a program that will generate an amortization schedule.
This should be an easy one: I am creating a program that spawns 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.