如何获得treeview中的子节点的全路径

砖头98 2020-05-21 10:26:53
如何在我点击treeview生成的子节点(文件text的文件名)后,获取到我点击的文件在文件夹中的文件路径?
...全文
690 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
The 祺℡ 2020-05-22
  • 打赏
  • 举报
回复
TreeView1_Click不是用这个。应该是用TreeView1_NodeMouseClick。 法1:如果父节点是文件路径,子节点是文件名 //添加: TreeNode parentNode = new TreeNode(@"C:\\"); //父节点是文件路径 TreeNode childNode = new TreeNode(@"1.txt"); //子节点是文件名 parentNode.Nodes.Add(childNode); treeView1.Nodes.Add(parentNode); //双击获取 private void TreeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { TreeNode childNode = e.Node; if (childNode.Level == 1) //判定点击的节点level是否是1,也就是是否是子节点 { MessageBox.Show(childNode.Parent.Text + childNode.Text); } } 法2:给节点加一个Tag标签 //添加 string fullPath = @"C:\\1.txt"; TreeNode childNode = new TreeNode(); //子节点是文件名 childNode.Text = Path.GetFileName(fullPath); childNode.Tag = fullPath; treeView1.Nodes.Add(childNode); //获取 private void TreeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { TreeNode childNode = e.Node; if (File.Exists(childNode.Tag.ToString())) { MessageBox.Show(childNode.Tag.ToString()); } }
砖头98 2020-05-22
  • 打赏
  • 举报
回复
引用 1 楼 贵阳老马马善福专业维修游泳池堵漏防水工程的回复:
string getPath(TreeNode tn)
{
string r = tn.Text;
while (tn.Parent != null)
{
tn = tn.Parent;
r = tn.Text + "/" + r;
}
return r;
}
private void TreeView1_Click(System.Object sender, System.EventArgs e) { TreeNode tn; string path; path = getPath(tn); using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read)) { byte[] buffer = new byte[5242880] { }; int r1 = fsRead.Read(buffer, 0, buffer.Length); TextBox1.Text = Encoding.UTF8.GetString(buffer, 0, r1); } } private string getPath(TreeNode tn) { string r = tn.Text; while (tn.Parent != null) { tn = tn.Parent; r = tn.Text + "/" + r; } return r; } 因为在学习,所以很多不懂,为什么我加上你的取全路径的函数后给出如下信息 警告 1 变量“tn”在赋值前被使用。可能会在运行时导致 null 引用异常。
砖头98 2020-05-22
  • 打赏
  • 举报
回复
引用 5 楼 贵阳老马马善福专业维修游泳池堵漏防水工程的回复:
[quote=引用 2 楼 砖头98 的回复:]
[quote=引用 1 楼 贵阳老马马善福专业维修游泳池堵漏防水工程的回复:]string getPath(TreeNode tn)
{
string r = tn.Text;
while (tn.Parent != null)
{
tn = tn.Parent;
r = tn.Text + "/" + r;
}
return r;
}


private void TreeView1_Click(System.Object sender, System.EventArgs e)
{
TreeNode tn;
string path;
path = getPath(tn);
using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
{
byte[] buffer = new byte[5242880] { };
int r1 = fsRead.Read(buffer, 0, buffer.Length);
TextBox1.Text = Encoding.UTF8.GetString(buffer, 0, r1);
}
}

private string getPath(TreeNode tn)
{
string r = tn.Text;
while (tn.Parent != null)
{
tn = tn.Parent;
r = tn.Text + "/" + r;
}
return r;
}

因为在学习,所以很多不懂,为什么我加上你的取全路径的函数后给出如下信息

警告 1 变量“tn”在赋值前被使用。可能会在运行时导致 null 引用异常。[/quote]

TreeNode tn;
->
TreeNode tn = treeView1.SelectedNode;
或者在 NodeClick里面
TreeNode tn = e.Node;[/quote] 好的,谢谢大神,晚上回家试试,非常感谢您的再次解疑
threenewbee 2020-05-22
  • 打赏
  • 举报
回复
引用 2 楼 砖头98 的回复:
[quote=引用 1 楼 贵阳老马马善福专业维修游泳池堵漏防水工程的回复:]string getPath(TreeNode tn)
{
string r = tn.Text;
while (tn.Parent != null)
{
tn = tn.Parent;
r = tn.Text + "/" + r;
}
return r;
}


private void TreeView1_Click(System.Object sender, System.EventArgs e)
{
TreeNode tn;
string path;
path = getPath(tn);
using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
{
byte[] buffer = new byte[5242880] { };
int r1 = fsRead.Read(buffer, 0, buffer.Length);
TextBox1.Text = Encoding.UTF8.GetString(buffer, 0, r1);
}
}

private string getPath(TreeNode tn)
{
string r = tn.Text;
while (tn.Parent != null)
{
tn = tn.Parent;
r = tn.Text + "/" + r;
}
return r;
}

因为在学习,所以很多不懂,为什么我加上你的取全路径的函数后给出如下信息

警告 1 变量“tn”在赋值前被使用。可能会在运行时导致 null 引用异常。[/quote]

TreeNode tn;
->
TreeNode tn = treeView1.SelectedNode;
或者在 NodeClick里面
TreeNode tn = e.Node;
砖头98 2020-05-22
  • 打赏
  • 举报
回复
引用 3 楼 The 祺℡的回复:
TreeView1_Click不是用这个。应该是用TreeView1_NodeMouseClick。 法1:如果父节点是文件路径,子节点是文件名 //添加: TreeNode parentNode = new TreeNode(@"C:\\"); //父节点是文件路径 TreeNode childNode = new TreeNode(@"1.txt"); //子节点是文件名 parentNode.Nodes.Add(childNode); treeView1.Nodes.Add(parentNode); //双击获取 private void TreeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { TreeNode childNode = e.Node; if (childNode.Level == 1) //判定点击的节点level是否是1,也就是是否是子节点 { MessageBox.Show(childNode.Parent.Text + childNode.Text); } } 法2:给节点加一个Tag标签 //添加 string fullPath = @"C:\\1.txt"; TreeNode childNode = new TreeNode(); //子节点是文件名 childNode.Text = Path.GetFileName(fullPath); childNode.Tag = fullPath; treeView1.Nodes.Add(childNode); //获取 private void TreeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { TreeNode childNode = e.Node; if (File.Exists(childNode.Tag.ToString())) { MessageBox.Show(childNode.Tag.ToString()); } }
谢谢大神!我试试
threenewbee 2020-05-21
  • 打赏
  • 举报
回复
string getPath(TreeNode tn)
{
string r = tn.Text;
while (tn.Parent != null)
{
tn = tn.Parent;
r = tn.Text + "/" + r;
}
return r;
}

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧