简体   繁体   English

我的for陈述无法正常运作

[英]My for statement isn't working properly

I have tried so much different iterations of the for loops and all of them dont work properly or work only halfway. 我已经尝试了for循环的许多不同迭代,但它们都无法正常工作或只能中途工作。 So I cant figure it out. 所以我不知道。 Heres my form: 这是我的表格:

在此处输入图片说明

So what its supposed to do is create a folder for each node in the treeview (Named FolderTV) when I click the Create folders button. 因此,当我单击“创建文件夹”按钮时,应该为树视图中的每个节点创建一个文件夹(名为FolderTV)。 What it currently does is only creates the New_Mod folder and the Data folder nothing else. 它当前所做的只是创建New_Mod文件夹和Data文件夹而已。 I want it to create a folder for every node and subnode. 我希望它为每个节点和子节点创建一个文件夹。 For example it would create the New_Mod folder and then within that folder It will create the Data, Models, and Textures folder and within the Data folder it would create a Scripts folder. 例如,它将创建New_Mod文件夹,然后在该文件夹中创建Data,Models和Textures文件夹,并在Data文件夹中创建一个Scripts文件夹。

Here's my code for that button: 这是该按钮的代码:

private void button3_Click(object sender, EventArgs e)    
    {
        for (int i = 0; i <= FoldersTV.Nodes.Count; i++)
        {

            FoldersTV.SelectedNode = FoldersTV.TopNode;
            MessageBox.Show("Current Node: " + FoldersTV.SelectedNode.Text.ToString());
            Directory.CreateDirectory(SEAppdata + "\\" + FoldersTV.SelectedNode.Text.ToString());
            for (int x = 0; x <= FoldersTV.Nodes.Count; x++)
            {
                TreeNode nextNode = FoldersTV.SelectedNode.NextVisibleNode;
                MessageBox.Show("Next Node: " + nextNode.Text.ToString());
                Directory.CreateDirectory(SEAppdata + "\\" + FoldersTV.SelectedNode.Text.ToString() + "\\" + nextNode.Text.ToString());
                x++;
            }
            i++;
        }

    }

Try this (untested) recursive solution and try to understand it first ;) 尝试此(未试用的)递归解决方案,并首先尝试理解它;)

private void CreateDirs(string path, IEnumerable<TreeNode> nodes) {
    foreach(var node in nodes) {
    //  string dir = path + "\\" + node.Text;
        string dir = Path.Combine(path, node.Text);
        Directory.CreateDirectory(dir);
        CreateDirs(dir, node.Nodes);
    }
}

private void button3_Click(object sender, EventArgs e) {
    CreateDirs(SEAppdata, FoldersTV.Nodes);
}

EDIT: Version with few debug printouts: 编辑:带有少量调试打印输出的版本:

// using System.Diagnostics;
private void CreateDirs(string path, IEnumerable<TreeNode> nodes) {

//  iterate through each node (use the variable `node`!)
    foreach(var node in nodes) {
    //  combine the path with node.Text
        string dir = Path.Combine(path, node.Text);

    //  create the directory + debug printout
        Debug.WriteLine("CreateDirectory({0})", dir);
        Directory.CreateDirectory(dir);

    //  recursion (create sub-dirs for all sub-nodes)
        CreateDirs(dir, node.Nodes);
    }
}

See: System.Diagnostics.Debug.WriteLine 请参阅: System.Diagnostics.Debug.WriteLine

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM