首页 > 代码库 > TreeNode(包含读出文件里的信息)
TreeNode(包含读出文件里的信息)
public partial class Tree : Form
{
public Tree()
{
InitializeComponent();
}
private void Tree_Load(object sender, EventArgs e)
{
string path = @"d:\a\";
TreeNode tr = this.TV.Nodes.Add(Directory.GetDirectoryRoot(path));
TreeNode node1 = new TreeNode();
LoadData(path,tr.Nodes);
}
private void LoadData(string path, TreeNodeCollection tr)
{
string[] strs = Directory.GetDirectories(path);
foreach(var item in strs)
{
Tag = Path.GetFileName(item); // 获取或设置包含有关控件的数据的对象。 返回 一个 System.Object,它包含有关控件的数据。 默认值为 null。
TreeNode tr1 = tr.Add(item ,Tag.ToString());
LoadData(item,tr1.Nodes);
}
string[] strs2 = Directory.GetFiles(path);
foreach(var item in strs2)
{
if(Path.GetExtension(item)==".txt")
{
Tag = Path.GetFileName(item);
tr.Add(item,Tag.ToString());
}
}
}
private void TV_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
if(this.TV.SelectedNode!=null)
{
string path = this.TV.SelectedNode.Name.ToString();
if(path.LastIndexOf(".txt")>0)
{
FileStream stream = new FileStream(path,FileMode.Open);
StreamReader sr = new StreamReader(stream,Encoding.Default);
this.richTextBox1.Text = sr.ReadToEnd();
sr.Close();
stream.Close();
}
}
}
}
TreeNode(包含读出文件里的信息)