下个winfrom treeview显示目录树例子要50C币下了还用不上自己写个只要5C币

早上赶工要搞个treeview显示树状目录,好久没用treeview了到csdn下载了个需要50c币,现在自己写了个,效果还行,下载Demo只需要5C币

效果如下:

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.Windows.Forms;

namespace TreeViewDemo
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// 当前选中的文件路径
        /// </summary>
        private string currentfilePath="";
        public Form1()
        {
            InitializeComponent();
            this.treeView1.ImageList = imageList1;
            TreeNode node = new TreeNode();
            string path = @"C:\";
            this.MakeTreeViewNodes(node, path);
            node.Text = "目录";
            this.treeView1.Nodes.Add(node);
        }
        /// <summary>
        /// 当某个节点被展开的时候则从该节点开始往下遍历2级目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
        {
            string dirPath = e.Node.Name;
            if (string.IsNullOrEmpty(dirPath))
            {
                return;
            }
            if (e.Node.Tag != null && e.Node.Tag.ToString() != "Dir")
            {
                return;
            }
            this.MakeTreeViewNodes(e.Node, dirPath);
        }
        /// <summary>
        /// 动态生成treeview节点
        /// </summary>
        /// <param name="node"></param>
        /// <param name="path"></param>
        private void MakeTreeViewNodes(TreeNode node, string path)
        {
            node.Nodes.Clear();
            DirectoryInfo directoryInfo = new DirectoryInfo(path);
            var fileArr = directoryInfo.GetFiles("*.*");
            if (fileArr != null && fileArr.Length > 0)
            {
                for (int i = 0; i < fileArr.Length; i++)
                {
                    TreeNode tn = new TreeNode();
                    tn.Name = fileArr[i].FullName;
                    tn.Text = Path.GetFileName(fileArr[i].FullName); //显示名称
                    tn.Tag = "File";
                    tn.ImageIndex = 1;
                    node.Nodes.Add(tn);
                }
            }
            var dirArr = directoryInfo.GetDirectories();
            if (dirArr != null && dirArr.Length > 0)
            {
                foreach (var item in dirArr)
                {
                    //过滤隐藏文件夹
                    if (item.Attributes.ToString().IndexOf("Hidden") > -1) // && item.Attributes.ToString().IndexOf("System") > -1
                    {
                        continue;
                    }
                    TreeNode tn = new TreeNode();
                    tn.Name = item.FullName;
                    tn.Text = item.Name;
                    tn.Tag = "Dir";
                    tn.ImageIndex = 0;
                    if (item.GetFiles().Length > 0||item.GetDirectories().Length>0)
                    {
                        TreeNode tt = new TreeNode();
                        tn.Nodes.Add(tt);
                    }
                    node.Nodes.Add(tn);
                }
            }
        }
        /// <summary>
        /// 单击选中node 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            var node = e.Node as TreeNode;
            if (node == null)
            {
                return;
            }
            if (node.Tag != null && node.Tag.ToString() == "File")
            {
                if (!string.IsNullOrEmpty(node.Name))
                {
                    if (!currentfilePath.Equals(node.Name))
                    {
                        node.SelectedImageIndex = 1;
                        //你可以在此根据文件具体类型 打开或者其他操作
                        currentfilePath = node.Name;
                    }
                }
            }
        }
    }
}

下载链接:https://download.csdn.net/download/asa_jim/11579674

 要是csdn给涨所需下载币了,劳烦回复下 我重新上传个!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凌晨4点5杀老大爷

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值