码迷,mamicode.com
首页 > 其他好文 > 详细

TreeView、ListView显示电脑的文件夹及文件

时间:2018-09-20 01:05:04      阅读:373      评论:0      收藏:0      [点我收藏+]

标签:nta   vat   directory   private   clear   namespace   tno   forms   file   

TreeView、ListView显示电脑的文件夹及文件

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 TraversMagneticDiskCatalogue
{
    public partial class Form1 : Form
    {
        TreeNode selectNode;
        public Form1()
        { 
            InitializeComponent();
            lstViewInfo.View = View.List;
            this.Load += Form1_Load;
            trViewInfo.AfterSelect += TrViewInfo_AfterSelect;
            trViewInfo.NodeMouseDoubleClick += TrViewInfo_NodeMouseDoubleClick;
            contextMenuStrip1.Click += ContextMenuStrip1_Click;
        }

        private void ContextMenuStrip1_Click(object sender, EventArgs e)
        {
            string path = selectNode+ lstViewInfo.SelectedItems[0].Text;
            System.Diagnostics.Process.Start(path);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lstViewInfo.ContextMenuStrip = contextMenuStrip1;
            trViewInfo.ImageList = imageList1;
            TreeNode rootNode = trViewInfo.Nodes.Add("我的电脑", "我的电脑", 1, 0);
            //TreeViewShow(rootNode,rootNode.Tag.ToString());
        }

        private void TrViewInfo_AfterSelect(object sender, TreeViewEventArgs e)
        {
            selectNode = new TreeNode((string)e.Node.Tag);
            TreeViewShow(e.Node,(string)e.Node.Tag);
            ListViewShow(e.Node);
        }
        private void TreeViewShow(TreeNode subNode,string path)
        {
            try
            {
                if (subNode.Nodes.Count == 0)
                {
                    if (subNode.Parent == null)
                    {
                        foreach (string driverName in Directory.GetLogicalDrives())     //获取电脑逻辑盘
                        {
                            TreeNode driverNode = new TreeNode();
                            driverNode.Tag = driverName;
                            driverNode.Text = driverName.Substring(0, 1);
                            driverNode.SelectedImageIndex = 2;
                            driverNode.ImageIndex = 3;
                            subNode.Nodes.Add(driverNode);
                        }
                    }
                    else
                    {
                        DirectoryInfo directoryInfo = new DirectoryInfo(path);
                        int countDir = Directory.GetDirectories(path).Length;
                            foreach (DirectoryInfo  dirName in directoryInfo.GetDirectories())    //获取文件夹
                            {
                                TreeNode dirNode = new TreeNode();
                                dirNode.Tag = dirName.FullName;
                                dirNode.Text = dirName.Name;
                                dirNode.ImageIndex = 4;
                                dirNode.SelectedImageIndex =4;
                                subNode.Nodes.Add(dirNode);
                            }
                        DirectoryInfo dirinfo = new DirectoryInfo(path);
                        //int countfiles = Directory.GetFiles(path).Length;
                            foreach (FileInfo fileName in dirinfo.GetFiles("*.*"))    //获取文件
                            {
                                TreeNode dirNode = new TreeNode();
                                dirNode.Tag = fileName.FullName;
                                dirNode.Text = fileName.Name;
                                dirNode.ImageIndex = 6;
                                dirNode.SelectedImageIndex = 6;
                                subNode.Nodes.Add(dirNode);
                            }
                       }
                }
            }
            catch
            {
            } 
        }
        private void ListViewShow(TreeNode treeNode)
        {
            lstViewInfo.Items.Clear();
            try
            {
                if (treeNode.Parent == null)
                {
                    foreach (DriveInfo driver in DriveInfo.GetDrives())
                    {
                        ListViewItem viewItem = new ListViewItem(driver.Name.Substring(0, 1));
                        lstViewInfo.Items.Add(viewItem);
                    }
                }
                else
                {
                    DirectoryInfo directory = new DirectoryInfo((string)treeNode.Tag);
                    foreach (DirectoryInfo dir in directory.GetDirectories())
                    {
                        ListViewItem viewItem = new ListViewItem(dir.Name);
                        lstViewInfo.Items.Add(viewItem);
                    }

                    foreach (FileInfo file in directory.GetFiles())
                    {
                        ListViewItem viewItem = new ListViewItem(file.Name);
                        lstViewInfo.Items.Add(viewItem);
                    }
                }
            }
            catch { }
        }

        private void TrViewInfo_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            DirectoryInfo directory = new DirectoryInfo((string)e.Node.Tag);
            FileInfo fileInfo = new FileInfo((string)e.Node.Tag);
            if (directory.Exists)
            {
                folderBrowserDialog1.SelectedPath = (string)e.Node.Tag;
                folderBrowserDialog1.ShowDialog();
            }
            if(fileInfo.Exists)
            {
                System.Diagnostics.Process.Start((string)e.Node.Tag);
            }
        }
    }
}

效果

技术分享图片

TreeView、ListView显示电脑的文件夹及文件

标签:nta   vat   directory   private   clear   namespace   tno   forms   file   

原文地址:https://www.cnblogs.com/wenjie0904/p/9678280.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!