获取当前目录树视图选择在C# [英] 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?
就像如果我有树
根
=>儿童
root1下
=> Child1
所以,当我选择根和添加文件夹应该被添加到根以及。同名文件夹,应添加到根文件夹所在目录
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.
推荐答案
检查:
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:我假设你正在输入你的目录或文件名的文本框进和树节点的文本包含您的目录路径,我不知道你是怎么配置的TreeView
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#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!