简体   繁体   中英

Get Current Directory of Selected Treeview in c#

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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