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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:13:12+00:00 2026-05-27T19:13:12+00:00

I have a string array of some file paths: path/to/folder/file.xxx path/to/other/ path/to/file/file.xx path/file.x path/

  • 0

I have a string array of some file paths:

path/to/folder/file.xxx
path/to/other/
path/to/file/file.xx
path/file.x
path/

How can I convert this list to a tree structure? So far I have the following:

/// <summary>
/// Enumerates types of filesystem nodes.
/// </summary>
public enum FilesystemNodeType
{
    /// <summary>
    /// Indicates that the node is a file.
    /// </summary>
    File,

    /// <summary>
    /// Indicates that the node is a folder.
    /// </summary>
    Folder
}

/// <summary>
/// Represents a file or folder node.
/// </summary>
public class FilesystemNode
{
    private readonly ICollection<FilesystemNode> _children; 

    /// <summary>
    /// Initializes a new instance of the <see cref="FilesystemNode"/> class.
    /// </summary>
    public FilesystemNode()
    {
        _children = new LinkedList<FilesystemNode>();
    }

    /// <summary>
    /// Gets or sets the name of the file or folder.
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// Gets or sets the full path to the file or folder from the root.
    /// </summary>
    public string Path { get; set; }

    /// <summary>
    /// Gets or sets a value indicating whether the node is a file or folder.
    /// </summary>
    public FilesystemNodeType Type { get; set; }

    /// <summary>
    /// Gets a list of child nodes of this node. The node type must be a folder to have children.
    /// </summary>
    public ICollection<FilesystemNode> Children
    {
        get
        {
            if (Type == FilesystemNodeType.Folder)
                return _children;

            throw new InvalidOperationException("File nodes cannot have children");
        }
    }
}

I’m just a bit at a loss at how to actually split up the paths and all. Any path that ends with a / is a directory, any one that doesn’t, is not.

Also, while my input will always contain a path to the folder, how would I account for that situation if it did not?

For example, if I had the input:

path/to/file.c
path/file.c
path/

How would I account for the fact that path/to/ is not in the input?

  • 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-27T19:13:13+00:00Added an answer on May 27, 2026 at 7:13 pm

    Here is a solution that generates a nested dictionary of NodeEntry items (you can substitute your file info class as needed):

    public class NodeEntry
    {
        public NodeEntry()
        {
            this.Children = new NodeEntryCollection();
        }
    
        public string Key { get; set; }
        public NodeEntryCollection Children { get; set; }
    
    }
    
    public class NodeEntryCollection : Dictionary<string, NodeEntry>
    {
        public void AddEntry(string sEntry, int wBegIndex)
        {
            if (wBegIndex < sEntry.Length)
            {
                string sKey;
                int wEndIndex;
    
                wEndIndex = sEntry.IndexOf("/", wBegIndex);
                if (wEndIndex == -1)
                {
                    wEndIndex = sEntry.Length;
                }
                sKey = sEntry.Substring(wBegIndex, wEndIndex - wBegIndex);
                if (!string.IsNullOrEmpty(sKey)) {
                    NodeEntry oItem;
    
                    if (this.ContainsKey(sKey)) {
                        oItem = this[sKey];
                    } else {
                        oItem = new NodeEntry();
                        oItem.Key = sKey;
                        this.Add(sKey, oItem);
                    }
                    // Now add the rest to the new item's children
                    oItem.Children.AddEntry(sEntry, wEndIndex + 1);
                }
            }
        }
    }
    

    To use the above, create a new collection:

            NodeEntryCollection cItems = new NodeEntryCollection();
    

    then, for each line in your list:

            cItems.AddEntry(sLine, 0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I have a string array of some random saying and the like, but
I have an array of strings: var matchThese = [Jabberwocky, Harry Potter, Some other];
I have a string array in C named args[] - now how can I
I have a file that looks like this (but is much bigger): >some text
So, deserializing is working however I have some xml like this: <File> <Stuff>stuff</Stuff> <Devices>
I have a site where a user can download a file. Some files are
I have some String[] arrays, for example: ['a1', 'a2'] ['b1', 'b2', 'b3', 'b4'] ['c1']
I have a mutable array that is retained and storing several objects. At some
I have a string array in C# and I intend to copy it in
I have a string array: string[] authors = new string[3]; authors[0] = Charles Dickens;

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.