Click here to Skip to main content
15,888,908 members
Home / Discussions / C#
   

C#

 
QuestionPrinting graphics in C# with sizes in mm Pin
Member 1050678613-Apr-15 8:21
Member 1050678613-Apr-15 8:21 
AnswerRe: Printing graphics in C# with sizes in mm Pin
OriginalGriff13-Apr-15 8:26
mveOriginalGriff13-Apr-15 8:26 
Questionpassing a value that maybe null to a dialog form Pin
jkirkerx13-Apr-15 8:13
professionaljkirkerx13-Apr-15 8:13 
AnswerRe: passing a value that maybe null to a dialog form Pin
OriginalGriff13-Apr-15 8:22
mveOriginalGriff13-Apr-15 8:22 
GeneralRe: passing a value that maybe null to a dialog form Pin
jkirkerx13-Apr-15 8:41
professionaljkirkerx13-Apr-15 8:41 
QuestionENVDTE2 Question Pin
Kevin Marois13-Apr-15 8:03
professionalKevin Marois13-Apr-15 8:03 
QuestionTree class recommendation Pin
DaveyM6913-Apr-15 6:05
professionalDaveyM6913-Apr-15 6:05 
AnswerRe: Tree class recommendation Pin
Eddy Vluggen13-Apr-15 8:58
professionalEddy Vluggen13-Apr-15 8:58 
DaveyM69 wrote:
If anyone could point me in the right direction it would be much appreciated!
You'd need a class that can hold a reference to itself (the parent) and a collection of its childeren. Like below;
C#
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

class Program
{
    public class Folder
    {
        Collection<Folder> _children;
        public Folder Parent { get; private set; }
        public string Name { get; set; }
        public Collection<Folder> Children { get { return _children; } }

        private Folder(Folder parent, string name)
        {
            _children = new Collection<Folder>();
            Parent = parent;
            Name = name;
        }

        public static Folder CreateNewRoot()
        {
            return new Folder(null, "Root");
        }

        public Folder CreateFolder(string name)
        {
            var result = new Folder(this, name);
            this.Children.Add(result);
            return result;
        }

        public override string ToString()
        {
            Folder currentFolder = this;

            var pathBuilder = new StringBuilder();
            while (currentFolder != null)
            {
                pathBuilder.Insert(0, currentFolder.Name);
                pathBuilder.Insert(0, '/');
                currentFolder = currentFolder.Parent;
            }

            return pathBuilder.ToString();
        }
    }

    static Folder CreatePath(Folder startingFolder, string path)
    {
        string[] pathParts = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
        Folder currentFolder = startingFolder;
        for (int i = 0; i < pathParts.Length; i++)
        {
            string targetName = pathParts[i];
            // does current folder hold our target?
            Folder targetFolder = null;
            if (0 != currentFolder.Children.Count())
                targetFolder = currentFolder.Children.FirstOrDefault(f => f.Name == targetName);
            if (null == targetFolder)
            {
                // it don't exist yet
                targetFolder = currentFolder.CreateFolder(targetName);
            }
            // on to the next one
            currentFolder = targetFolder;
        }

        return currentFolder;
    } 

    static void DumpFolderToConsole(Folder fromWhere)
    {
        Console.WriteLine(fromWhere);
        foreach (Folder folder in fromWhere.Children)
        {
            DumpFolderToConsole(folder);
        }
    }

    static void Main(string[] args)
    {
        Folder root = Folder.CreateNewRoot();
        CreatePath(root, @"/ch");
        CreatePath(root, @"/ch/01/config");
        CreatePath(root, @"/ch/02/insert");
        CreatePath(root, @"/ch/01/mix/01");
        CreatePath(root, @"/config/chlink");

        DumpFolderToConsole(root);
        Console.ReadLine();
    }
}
Outputs
/Root
/Root/ch
/Root/ch/01
/Root/ch/01/config
/Root/ch/01/mix
/Root/ch/01/mix/01
/Root/ch/02
/Root/ch/02/insert
/Root/config
/Root/config/chlink
Some remarks; the example also creates sub-folders if required, even if the starting list was shorter than the resulting dump. It does not contain provisions for other stuff yet, like files. If you want such a structure (complete with a byte[] for the file-class), then there's an example in an article[^] I wrote.

--edit
You asked for a class, not for code; I'd recommend the TreeNode, as it contains a lot what you'd want out of the box. Another advantage thereof would be that it'd be very easy to visualize in a TreeView.

---- edit
PO'H - I unchecked the "Use Markdown formatting" checkbox for you. This fixed the odd layout issues in your code block.
Bastard Programmer from Hell Suspicious | :suss:
If you can't read my code, try converting it here[^]


modified 14-Apr-15 8:47am.

GeneralRe: Tree class recommendation Pin
DaveyM6913-Apr-15 12:46
professionalDaveyM6913-Apr-15 12:46 
GeneralRe: Tree class recommendation Pin
Eddy Vluggen13-Apr-15 12:54
professionalEddy Vluggen13-Apr-15 12:54 
GeneralRe: Tree class recommendation Pin
BillWoodruff13-Apr-15 14:20
professionalBillWoodruff13-Apr-15 14:20 
GeneralRe: Tree class recommendation Pin
BillWoodruff13-Apr-15 13:22
professionalBillWoodruff13-Apr-15 13:22 
GeneralRe: Tree class recommendation Pin
Eddy Vluggen13-Apr-15 21:51
professionalEddy Vluggen13-Apr-15 21:51 
GeneralRe: Tree class recommendation Pin
DaveyM6914-Apr-15 8:26
professionalDaveyM6914-Apr-15 8:26 
GeneralRe: Tree class recommendation Pin
Eddy Vluggen14-Apr-15 9:02
professionalEddy Vluggen14-Apr-15 9:02 
AnswerRe: Tree class recommendation Pin
Mycroft Holmes13-Apr-15 14:29
professionalMycroft Holmes13-Apr-15 14:29 
Questionc# and xml Pin
Sandeep Puvvadi13-Apr-15 1:47
Sandeep Puvvadi13-Apr-15 1:47 
Answer[Repost] Pin
Sascha Lefèvre13-Apr-15 1:58
professionalSascha Lefèvre13-Apr-15 1:58 
QuestionFixed delete SQL Server database records ? Pin
Member 245846712-Apr-15 18:44
Member 245846712-Apr-15 18:44 
AnswerRe: Fixed delete SQL Server database records ? Pin
OriginalGriff12-Apr-15 21:31
mveOriginalGriff12-Apr-15 21:31 
QuestionIs there any document oriented database with file store which can work both on windows and windows 8.1 phone platforms? Pin
thomas anderson11-Apr-15 22:41
thomas anderson11-Apr-15 22:41 
AnswerRe: Is there any document oriented database with file store which can work both on windows and windows 8.1 phone platforms? Pin
BillWoodruff13-Apr-15 3:59
professionalBillWoodruff13-Apr-15 3:59 
Questionnever done c# before Pin
Member 1159958411-Apr-15 12:40
Member 1159958411-Apr-15 12:40 
AnswerRe: never done c# before Pin
Sascha Lefèvre11-Apr-15 12:51
professionalSascha Lefèvre11-Apr-15 12:51 
GeneralRe: never done c# before Pin
Member 1159958411-Apr-15 13:21
Member 1159958411-Apr-15 13:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.