首页 > 其他 > 详细

TreeView目录

时间:2018-02-01 11:25:37      阅读:175      评论:0      收藏:0      [点我收藏+]

效果图 

技术分享图片

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
string path = @"F:\aa"; //获取地址
LoadData(path,treeView1.Nodes);

}

private void LoadData(string path, TreeNodeCollection treeNodeCollection)
{
//1.获取子目录
string[] dirs = Directory.GetDirectories(path);
foreach (var item in dirs)
{
TreeNode tn= treeNodeCollection.Add(Path.GetFileName(item));
LoadData(item,tn.Nodes);
}
//2.获取子文件

string[] files = Directory.GetFiles(path);
foreach (var item in files)
{
TreeNode node = treeNodeCollection.Add(Path.GetFileName(item));
node.Tag = item;

}


}

private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) //双击treeview事件
{
if(e.Node.Tag!=null){ //判断节点内容是否为空
textBox1.Text = File.ReadAllText(e.Node.Tag.ToString(),Encoding.Default);
}
}


}
}

TreeView目录

原文:https://www.cnblogs.com/Jessica-insist/p/8398059.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!