1

I saw really many examples displaying exisiting folder structure with TreeView, but what I am trying to do is following ->

I have a standard folder structure in my C# WinForms with several folders and subfolders and a comboBox which display exisiting folders in my path.

The user should be able to choose a exisiting folder from my comboBox and just press a button to create every checked Item in my TreeView.

This is my existing Code:

    public Form1()
    {
        InitializeComponent();
        this.FormBorderStyle = FormBorderStyle.None;
        Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
        foreach (TreeNode tn in pathLorem.Nodes)
        {
            tn.Expand();
        }
        DirectoryInfo obj = new DirectoryInfo("F:\\");
        DirectoryInfo[] folders = obj.GetDirectories();
        loremDropDown.DataSource = folders;
    }

I don't beg for a finished code, I just need a tutorial or a exisiting StackOverflow post. I'm searching for 1 hour now.

enter image description here

  • 1
    I [searched](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-create-a-file-or-folder) for <1s.. - Ok.make it [2](https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control?view=netframeworkdesktop-4.8) – TaW Sep 06 '22 at 13:12
  • But that is not a TreeView solution ?? – TheCodingTentacle Sep 06 '22 at 13:14
  • The 2nd link shows how to enumerate, the 1st how to create files and fiolders. – TaW Sep 06 '22 at 13:15
  • The part which I can't figure out is how to get the names and the structure of the content in the TreeView to create the folders. – TheCodingTentacle Sep 06 '22 at 13:15
  • Well, we can't help as we don't know what you have stuffed into the nodes. Hint: To avoid extra work, you may want to add a ready made path o to each node's Tag property. – TaW Sep 06 '22 at 13:16
  • https://stackoverflow.com/questions/6239544/populate-treeview-with-file-system-directory-structure – dr.null Sep 06 '22 at 13:19
  • @TaW I updated my question for image references. > I declared a (name) for every TreeView Item, if that is what you meant by node Tag property – TheCodingTentacle Sep 06 '22 at 13:22
  • @dr.null is it possible to implement this into my project ? – TheCodingTentacle Sep 06 '22 at 13:27
  • If that is what you need, to display the tree/branch of a selected directory in a combo box. If so, handle the `SelectedIndexChanged` event to clear the `TV` and call the recursive method from there to populate. [Here's](https://stackoverflow.com/a/34465546/14171304) another approach. – dr.null Sep 06 '22 at 13:34
  • No, I have my TreeView structure in my program - I want to put my TreeView structure into a directory in my PC and create the file structure in my PC. – TheCodingTentacle Sep 06 '22 at 13:39
  • Then TaW directed you to right thing. – dr.null Sep 06 '22 at 13:41
  • That's a console solution for the file creation not winforms – TheCodingTentacle Sep 06 '22 at 13:47
  • The Tag property is a multipurpose property of many classes and yu can use it to store anything you know you will need later, like the path to a PictureBox.Image file or whateer. Both the TreeView and each of its nodes has one and you can use it for storing e.g. a string or a dedicated class or structure.. – TaW Sep 06 '22 at 14:32
  • Your post is unclear: Do you want to create Nodes from Items the user pick from a dropdown (which may be what the post asks) or do you want to create folders in the file system (which what the title say..) ??? – TaW Sep 06 '22 at 14:34
  • I want to create folders on my PC with the structure of my TreeView. My Dropdown is the path selector. In the dropdown the user can choose between 10 different paths, to determine where on my PC the TreeView structure should be implemented – TheCodingTentacle Sep 06 '22 at 14:37

1 Answers1

1

Based on your clarification, you need to create from the checked nodes tree directory structure in a given destination path.

Edit the constructor as follows...

public Form1()
{
    InitializeComponent();
    this.FormBorderStyle = FormBorderStyle.None;
    Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
    foreach (TreeNode tn in pathLorem.Nodes)
    {
        tn.Expand();
    }
    loremDropDown.DisplayMember = "Name";
    loremDropDown.ValueMember = "FullName";
    loremDropDown.DataSource = new DirectoryInfo("F:\\").GetDirectories();
}

Create a recursive method to get the checked nodes.

private IEnumerable<TreeNode> GetCheckedNodes(TreeNodeCollection nodeCol)
{
    foreach (TreeNode node in nodeCol)
    {
        if (node.Checked ||
            node.Nodes.Cast<TreeNode>().Any(n => n.Checked))
        {
            yield return node;
        }

        foreach (TreeNode childNode in GetCheckedNodes(node.Nodes))
        {
            if (childNode.Checked)
                yield return childNode;
        }
    }
}

To create the directories, Path.Combine the destination path and the TreeNode.FullPath and replace TreeView.PathSeparator with Path.DirectorySeparatorChar.

private void SomeButton_Click(object sender, EventArgs e)
{
    var destPath = loremDropDown.SelectedValue.ToString();
    var treeSep = pathLorem.PathSeparator;
    var dirSep = Path.DirectorySeparatorChar.ToString();

    foreach (var node in GetCheckedNodes(pathLorem.Nodes))
    {
        var sPath = Path.Combine(destPath, node.FullPath.Replace(treeSep, dirSep));
        Directory.CreateDirectory(sPath);
    }
}
dr.null
  • 4,032
  • 3
  • 9
  • 12
  • at ´var sPath = Path.Combine(destPath´ i get this error: cant convert from object to string. – TheCodingTentacle Sep 07 '22 at 09:12
  • Because I made this var destPath = loremDropDown.DataSource; – TheCodingTentacle Sep 07 '22 at 09:15
  • @TheCodingTentacle `destPath = loremDropDown.SelectedItem.ToString();`. Or just `destPath = loremDropDown.Text;`. – dr.null Sep 07 '22 at 10:07
  • @TheCodingTentacle What is the problem? Tell me what does not work? – dr.null Sep 07 '22 at 11:29
  • It doesn't create the files – TheCodingTentacle Sep 07 '22 at 11:35
  • @TheCodingTentacle What files? No files here, you mean folders/directories. Just tested it again and it works here. So double check. Use your debugger to see the `destPath` and `sPath` values. – dr.null Sep 07 '22 at 11:40
  • destPath is only the path folder name not containing the F:\\ before – TheCodingTentacle Sep 07 '22 at 11:49
  • sPath is also without F:\\ aswell – TheCodingTentacle Sep 07 '22 at 11:53
  • @TheCodingTentacle Yes you are right. You need to set the `DisplayMember` and `ValueMember` properties then you can get the `SelectedValue` which is the selected path. One second... – dr.null Sep 07 '22 at 11:59
  • But its not checking if the Item in the TreeView is checked, because when I remove the checkmark it's still generating it as a folder – TheCodingTentacle Sep 07 '22 at 12:22
  • 1
    @TheCodingTentacle Make sure to uncheck any checked children. You can't create a tree structure for a child node without it's parent. – dr.null Sep 07 '22 at 12:26
  • @TheCodingTentacle See also [this](https://stackoverflow.com/a/65425515/14171304) you can use it to auto check the parent and/or child nodes of a checked node... – dr.null Sep 07 '22 at 12:36
  • thank you. I want to create a shortcut on a other driver of the created folder - for example on Z:\\ - which has the same destination but just on a other driver – TheCodingTentacle Sep 07 '22 at 12:52
  • For example now I choose Lorem on my Dropdown so it will create the folder structure in F:\\Lorem - Now I want to create a shortcut of this on Z:\\Lorem – TheCodingTentacle Sep 07 '22 at 12:54
  • @TheCodingTentacle This is another question. See [Creating a shortcut to a folder in c#](https://stackoverflow.com/a/13019679/14171304). – dr.null Sep 07 '22 at 13:17
  • when I would add a second treeview in the same form which has the same path but on a other driver on Z for example - Then I would need to change destPaths string right ? – TheCodingTentacle Sep 08 '22 at 06:27
  • @TheCodingTentacle Yes, `destPath` is the destination dir that you select from the combo box like `F:\\SomeFolder`. If you want to do the same in another drive then you need to hard code that or refill the combo box with directories of the new drive. like: `loremDropDown.DataSource = new DirectoryInfo("Z:\\").GetDirectories();` Note `Z:` instead of `F:`. – dr.null Sep 08 '22 at 10:03
  • So I would need to do a second foreach for my GetCheckedNodes and declare a new var for it instead of destPath lets say testPath and make it like `var testPath = loremDropDown.DataSource = new DirectoryInfo("Z:\\").GetDirectories();` ? Because the folder name in my dropdown is always the same on both drivers but if I do It like that It will just create the F: folders. – TheCodingTentacle Sep 08 '22 at 10:47
  • 1
    @TheCodingTentacle This become more complicated to fix here, please post a new question based on this one and clarify the new requirements. – dr.null Sep 08 '22 at 10:57