Get Current Directory of Selected Treeview in c#

2019-06-09 08:15发布

问题:

Hi how I want to know the directory info of the current selected node of treeview, so that I can add Folder to the specified (selected node) path?

Like If i have Tree Root =>Child Root1 =>Child1 So when i select Root and Add folder folder should be added to root as well as same name folder should be added to the directory where the Root folder exists.

回答1:

Check this:

 private void btnAddFolder_Click(object sender, EventArgs e)
    {
        if (treeView1.SelectedNode != null)
        {
            TreeNode fileNode = new TreeNode();
            fileNode.Text = txtFileName.Text; 
            treeView1.SelectedNode.Nodes.Add(fileNode);
            string rootPath = treeView1.SelectedNode.Text; //here is your root path change it if it's wrong

            Directory.CreateDirectory(rootPath + "\\" + txtFileName.Text);

            // File.Create(rootPath + "\\" + txtFileName.Text); //if you want create a file instead of direcroty use this
        }
    }

PS: I assume you are typing your Directory or fileName into Textbox and Treenode texts contains your Directory path,i don't know how you configure your treeview.



标签: c# treeview