[C#]TreeView控件使用之磁盘目录查看器

设计一个程序,获取所有逻辑盘并列出硬盘所有目录。获取系统中的所有逻辑盘,如果是软驱和光驱,则仅向树视图中添加节点,如果是硬盘,则除了向树视图中添加节点外还要列举出它的目录。

使用了TreeView控件及文件和目录操作类FileInfo和DirectoryInfo 。

//C#作业,做完存档

//WorldsList Soft
//20111,11,92 @Dormitory Room 15**
//Computer wangyun 0901080114
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace S8_2
{
    public partial class Directory : Form
    {
        public Directory()
        {
            InitializeComponent();
            treeViewDirectory.BeginUpdate();
            EnumDrivers();
            treeViewDirectory.EndUpdate();
        }

        private void EnumDrivers()
        {
            TreeNode rootNode = new TreeNode();
            rootNode.Text = "我的电脑";
            treeViewDirectory.Nodes.Add(rootNode);
            treeViewDirectory.SelectedNode = rootNode.FirstNode;
            DriveInfo[] allDrives = DriveInfo.GetDrives();
            int j = 0;
            try
            {
                foreach (DriveInfo d in allDrives)
                {
                    TreeNode tn = new TreeNode(d.Name);
                    tn.Tag = d.RootDirectory.FullName;//获取当前绝对路径
                    treeViewDirectory.Nodes[0].Nodes.Add(tn);
                    treeViewDirectory.Nodes[0].Nodes[j].Text = d.Name;
                    ShowDirsAndFiles(tn);
                    j++;
                }
            }
            catch (System.Exception )
            {
            }
        }

        private void ShowDirsAndFiles(TreeNode tn)
        {
            tn.Nodes.Clear();
            try
            {
                DirectoryInfo DirInfo = new DirectoryInfo(tn.Tag.ToString());
                if (!DirInfo.Exists)
                {
                    return;
                }
                else
                {
                    DirectoryInfo[] Dirs;
                    FileInfo[] files;
                    try
                    {
                        //获得节点下的所有文件夹
                        Dirs = DirInfo.GetDirectories();
                        files = DirInfo.GetFiles();
                    }
                    catch (Exception e)
                    {
                        return;
                    }
                    //添加子节点
                    foreach (DirectoryInfo Dir in Dirs)
                    {
                        TreeNode dir = new TreeNode(Dir.Name);
                        dir.Tag = Dir.FullName;
                        tn.Nodes.Add(dir);
                    }
                    foreach (FileInfo file in files)
                    {
                        TreeNode fileName = new TreeNode(file.Name);
                        tn.Nodes.Add(fileName);
                    }
                }
            }
            catch (System.Exception )
            {                        }
        }
        private void treeViewDirectory_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            treeViewDirectory.BeginUpdate();
            foreach (TreeNode tn in e.Node.Nodes)
            {
                ShowDirsAndFiles(tn);
            }
            treeViewDirectory.EndUpdate();
            //当打开某个节点的时候同时更新ListView1;
        }
    }
}

程序运行图:

[C#]TreeView控件使用之磁盘目录查看器_第1张图片

 


程序下载地址:1 .[[[]]] 2. 【【】】

你可能感兴趣的:([C#]TreeView控件使用之磁盘目录查看器)