TreeViewListView显示电脑的文件夹及文件

Posted wenjie0904

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TreeViewListView显示电脑的文件夹及文件相关的知识,希望对你有一定的参考价值。

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);
            }
        }
    }
}

效果

技术分享图片

以上是关于TreeViewListView显示电脑的文件夹及文件的主要内容,如果未能解决你的问题,请参考以下文章

电脑文件可以搜索到。但是文件夹不显示应该有的文件?

电脑新建文件或删除文件需要刷新才显示

电脑显示“无法启动此程序”丢失.dll文件怎么办

电脑屏幕花屏问题诊断及解决方案 --总结

Win10系统电脑开机后显示无法登录到你的账户解决办法(亲测)

什么电脑桌面插件可以显示电子相册啊?