仿资源管理器(非递归)

using System;                                        

using System.Collections.Generic;     

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

 

using System.IO;

 

namespace ExplorerDemo2

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            string[] drives = Directory.GetLogicalDrives();

            foreach (string drive in drives)

            {

                TreeNode tn = new TreeNode();  //例如c:/

                tn.Text =drive;

                tn.Name = drive;

                this.treeView1.Nodes.Add(tn);

                try

                {

                    string[] folders = Directory.GetDirectories(drive);

                    foreach (string folder in folders)

                    {

                        TreeNode tm=new TreeNode(); //例如 c:/windows

                        tm.Name = folder; //path

                        //MessageBox.Show(tm.Name); 

                        tm.Text = folder.Substring(folder.LastIndexOf("//")+1);

                        tn.Nodes.Add(tm); //添加驱动器下的所有文件夹

                        //tn.ExpandAll(); //展开所有

                    }

                }

                catch (Exception ex)

                {

                    MessageBox.Show(ex.Message);

                }

            }

        }

 

        private void treeView1_AfterExpand(object sender, TreeViewEventArgs e)

        {

            TreeNode tn = e.Node; //例如 c:/

            tn.Name = e.Node.Name;

            //MessageBox.Show(e.Node.Text);

            treeView1.SelectedNode = e.Node;

            tn.Nodes.Clear();  //避免重复添加

            try

            {

                string[] folders = Directory.GetDirectories(tn.Name);

                foreach (string folder in folders)

                {

                    TreeNode tm = new TreeNode(); //例如c:/windows

                    tm.Name = folder;

                    tm.Text = folder.Substring(folder.LastIndexOf("//")+1);

                    //MessageBox.Show(tm.Name+"|"+tm.Text);

                    tn.Nodes.Add(tm);  // c:/windows

 

 

                    string[] foldersons = Directory.GetDirectories(tm.Name);  //例如:windows/system32

                    foreach (string folderson in foldersons)

                    {

                        TreeNode sunNode = new TreeNode();

                        sunNode.Name = folderson;

                        sunNode.Text = folderson.Substring(folderson.LastIndexOf("//")+1);

                        tm.Nodes.Add(sunNode);

                    }

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }//利用nodename属性来保存路径,用nodetext属性来保存路径切割后的值(即文件夹的名字)

        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值