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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:13:35+00:00 2026-05-30T06:13:35+00:00

First, I did check this post but it is in Python, first, and second

  • 0

First, I did check this post but it is in Python, first, and second it appears to be actually making the directories, which I cannot do in this scenario.

Second, these are not directories that exist, nor can I create them.

I have an input in C# like this:

        List<string> filePaths = new List<string>();
        filePaths.Add(@"ProgramDir\InstallDir\Module1\mod1pack1.exe");
        filePaths.Add(@"ProgramDir\InstallDir\Module1\mod1pack2.exe");
        filePaths.Add(@"ProgramDir\InstallDir\Module2\mod2pack1.exe");
        filePaths.Add(@"ProgramDir\InstallDir\Module1\SubModule1\report1.rpt");
        filePaths.Add(@"SystemDir\DependencyDir\dependency1.dll");
        filePaths.Add(@"SystemDir\DependencyDir\dependency2.dll");

What I have been trying to do is create an object that represents this structure, such that it could be visualized like this:

-ProgramDir
    Installdir
        Module1
            mod1pack1.exe
            mod1pack2.exe
            -SubModule1
                 report1.rpt
        Module2
            mod2pack1.exe
-SystemDir
    -DependencyDir
         dependency1.dll
     dependency2.dll

What I have tried is various versions of the following, and I could really use some help to figure out where I’ve got it wrong.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SetFilePathList();
            DTree forest = new DTree();
            List<DTreeBranch> branches = new List<DTreeBranch>();
            foreach (string path in filePaths)
            {
                forest.GrowTree(path.Split('\\'), branches);
            }
            forest.SubBranches.AddRange(branches);
        }
        private static List<string> filePaths { get; set; }
        private static void SetFilePathList()
        {
            filePaths = new List<string>();
            filePaths.Add(@"ProgramDir\InstallDir\Module1\mod1pack1.exe");
            filePaths.Add(@"ProgramDir\InstallDir\Module1\mod1pack2.exe");
            filePaths.Add(@"ProgramDir\InstallDir\Module2\mod2pack1.exe");
            filePaths.Add(@"ProgramDir\InstallDir\Module1\SubModule1\report1.rpt");
            filePaths.Add(@"SystemDir\DependencyDir\dependency1.dll");
            filePaths.Add(@"SystemDir\DependencyDir\dependency2.dll");
        }
    }
    public class DTree
    {
        public List<DTreeBranch> SubBranches { get; set; }
        public string BranchName { get; set; }
        public DTree() { SubBranches = new List<DTreeBranch>(); }
        public DTreeBranch AddChildren(string[] childElements, DTreeBranch branch)
        {
            DTreeBranch childBranch;
            foreach (string element in childElements)
            {
                childBranch = new DTreeBranch();
                childBranch.BranchName = element;
                branch.SubBranches.Add(childBranch);
                var query = from q in childElements
                            where q != childBranch.BranchName
                            select q;
                AddChildren(query.ToArray<string>(), childBranch);
            }
            return branch;
        }
        public void GrowTree(string[] pathElements, List<DTreeBranch> Branches)
        {
            DTreeBranch result = Branches.Find(delegate(DTreeBranch b)
            {
                return b.BranchName == pathElements[0];
            });

            if (result == null)
            {
                DTreeBranch newRootBranch = new DTreeBranch();
                newRootBranch.BranchName = pathElements[0];
                Branches.Add(newRootBranch);
                GrowTree(pathElements, Branches);
            }
            else
            {
                var query = from q in pathElements
                            where q != result.BranchName
                            select q;
                DTreeBranch childBranch = AddChildren(query.ToArray<string>(), result);
                Branches.Add(childBranch);
            }
        }
    }
    public class DTreeBranch
    {
        public List<DTreeBranch> SubBranches { get; set; }
        public string BranchName { get; set; }
        public DTreeBranch()
        {
            SubBranches = new List<DTreeBranch>();
        }
    }
}

The main thing is that the output is only two layers deep. I guess what I’m saying is that the new elements are added to the depth, not the breadth, and I’m at a loss as to how to effectively work through this. I also think that I have way more code than I need.

Thanks in advance.

  • 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-30T06:13:37+00:00Added an answer on May 30, 2026 at 6:13 am

    I’m not sure exactly what our goals are, but a simple recursive parse will do it quite easily. Wrote this up, and hope it helps. You can make it significantly more fancy if you want, with DTrees and sub branches, or separate collections for Files and Directories, etc. I don’t really understand what all that code in there is for. If it has something to do with WIX, I’m sorry 😉 And you could always use something like this to parse it out into the tree, and then convert that sanely to a different format.

    • this assumes no duplicate leaf nodes (file names).
    • if that isn’t the case, just add a sanity check like for directories.

    The main “Node” class –

    public class Node
    {
        public string Name { get; set; }
        public bool IsDirectory { get; set; }
        public List<Node> Children = new List<Node>();
    
        internal void AddChildren(string f)
        {
            var dirs = Path.GetDirectoryName(f);
            if (string.IsNullOrEmpty(dirs))
            {
                // we are adding a file
                var file = Path.GetFileName(f);
                Children.Add(new Node {Name = file, IsDirectory = false});
            }
            else
            {
                // we are adding a directory
                var firstDir = dirs.Split(Path.DirectorySeparatorChar)[0];
                var childNode = Children.FirstOrDefault(d => d.Name == firstDir);
                if (childNode == null)
                {
                    childNode = new Node {Name = firstDir, IsDirectory = true};
                    Children.Add(childNode);
                }
    
                var subPath = f.Substring(firstDir.Length + 1);
                childNode.AddChildren(subPath);
            }
        }
    }
    

    Calling it is simple, like this:

    var filePaths = new List<string> { 
        @"ProgramDir\InstallDir\Module1\mod1pack1.exe",
        @"ProgramDir\InstallDir\Module1\mod1pack2.exe",
        @"ProgramDir\InstallDir\Module2\mod2pack1.exe",
        @"ProgramDir\InstallDir\Module1\SubModule1\report1.rpt",
        @"SystemDir\DependencyDir\dependency1.dll",
        @"SystemDir\DependencyDir\dependency2.dll",
    };
    
    var node = new Node { Name = "Root", IsDirectory = true };
    foreach (var f in filePaths )
    {
        node.AddChildren(f);
    }
    

    Printing it out (with indent per level, gives me this)

    public static void PrintNode(Node node, int indent)
    {
        if (indent > 0) // don't print out root directory (level 1). 
        {
            var ending = node.IsDirectory ? Path.DirectorySeparatorChar.ToString() : "*";
            Console.WriteLine("{0}{1}{2}", new string('\t', indent - 1), node.Name, ending);
        }
        node.Children.ForEach(n => PrintNode(n, indent + 1));
    }
    
    ProgramDir\
        InstallDir\
                Module1\
                        mod1pack1.exe*
                        mod1pack2.exe*
                        SubModule1\
                                report1.rpt*
                Module2\
                        mod2pack1.exe*
    SystemDir\
        DependencyDir\
                dependency1.dll*
                dependency2.dll*
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all I did gave a look at this one. But I didn't
Disclaimer: I did check other questions that seemed related, but this one is much
I did this: [User.first, User.last].to_xml and got this: <users type=array> <user> <created-at type=datetime>2010-03-16T06:40:51Z</created-at> <id
First let me say that I did see this article: How to remove AspxAutoDetectCookieSupport
I did the following to upper case the first letter in each word but
First of all, I did a search on this and was able to find
Fair warning: I am no expert, but I did manage to get this far.
I recently stumbled over this post , which introduces the collect method for Scala
for better understanding check out this link first: http://sxsw.usehipster.com/questions/where-are-the-best-breakfast-tacos-in-austin as you can see there,
Check this out First thing!! so this guy posts how to do this on

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.