如何制作树视图列表视图资源管理器 [英] How Do I Make The Tree View List View Explorer

查看:92
本文介绍了如何制作树视图列表视图资源管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#windows form创建一个类似app的资源管理器。到目前为止,我能够做的是当我从组合框中选择驱动器时,它会在树视图中显示项目,但不会在列表视图中显示它我基本上想做的是一个资源管理器从组合框获取驱动器并在树视图中显示所有目录,当用户在树视图中选择文件夹时,它会在列表视图中显示该文件夹的内容。我必须提交今天这个。已经通过alomost每个网站,但无法理解正确的事情。我的代码如下:



I am making an explorer like app in C# windows form.So far what i have been able to do is that when i select the drive from combo box it displays the items in tree view but is not displaying it in the list view panel.What i basically want to make is an explorer that gets the drive from combo box and display all directories in tree view and when user selects the folder in tree view it displays the contents f that folder in the list view.I have to submit this today. Have gone through alomost every website but couldnt understand the right thing.My code is as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace Assignment1b
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            DriveInfo[] drives = DriveInfo.GetDrives();
            for (int i = 0; i <= drives.Length - 1; i++)
            {
                comboBox1.Items.Add(drives[i].Name);
                comboBox1.SelectedIndex = -1;
            }
        }

        private void gobtn_Click(object sender, EventArgs e)
        {
            ShowTreeViewItems(pathtb.Text);
            ShowDirectoryItems(pathtb.Text);
        }

        private void listView1_ItemActivate(object sender, EventArgs e)
        {
            pathtb.Text = pathtb.Text + listView1.SelectedItems[0].Text + "\\";
            ShowTreeViewItems(pathtb.Text);
            ShowDirectoryItems(pathtb.Text);
        }
        private void ShowTreeViewItems(string Path)
        {
            try
            {
                DirectoryInfo CurrDir = new DirectoryInfo(Path);
                DirectoryInfo[] SubDirs = CurrDir.GetDirectories();
                FileInfo[] Files = CurrDir.GetFiles();

                treeView1.Nodes.Clear();

                TreeNode parentNode = new TreeNode();
                parentNode.Text = Path;
                parentNode.ForeColor = Color.Black;
                parentNode.BackColor = Color.White;
                parentNode.ImageIndex = 2;
                parentNode.SelectedImageIndex = 2;
                treeView1.Nodes.Add(parentNode);

                foreach (DirectoryInfo dir in SubDirs)
                {
                    TreeNode childNode = new TreeNode();
                    childNode.Text = dir.Name;
                    childNode.ForeColor = Color.Black;
                    childNode.BackColor = Color.White;
                    childNode.ImageIndex = 2;
                    childNode.SelectedImageIndex = 2;
                    parentNode.Nodes.Add(childNode);
                    ////////////////////////////////////////////////////////
                    //TreeNode currentNode = new TreeNode();
                    //currentNode.Text = childNode.FullPath;
                    //currentNode.ForeColor = Color.Black;
                    //currentNode.BackColor = Color.White;
                    //currentNode.ImageIndex = 2;
                    //currentNode.SelectedImageIndex = 2;
                    //childNode.Nodes.Add(currentNode);

                }

                foreach (FileInfo file in Files)
                {
                    TreeNode childNode = new TreeNode();
                    childNode.Text = file.Name;
                    childNode.ForeColor = Color.Black;
                    childNode.BackColor = Color.White;
                    childNode.ImageIndex = 0;
                    childNode.SelectedImageIndex = 0;
                    parentNode.Nodes.Add(childNode);
                    ////////////////////////////////////////////////////
                    //TreeNode currentNode = new TreeNode();
                    //currentNode.Text = file.Name; ;
                    //currentNode.ForeColor = Color.Black;
                    //currentNode.BackColor = Color.White;
                    //currentNode.ImageIndex = 0;
                    //currentNode.SelectedImageIndex = 0;
                    //childNode.Nodes.Add(currentNode);
                }
                ShowDirectoryItems(Path);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message.ToString());
            }
        }

        private void ShowDirectoryItems(string Path)
        {
            DirectoryInfo CurrDir = new DirectoryInfo(Path);
            DirectoryInfo[] SubDirs = CurrDir.GetDirectories();
            FileInfo[] Files = CurrDir.GetFiles();

            DateTime dateModified;

            listView1.Items.Clear();

            listView1.BeginUpdate();

            foreach (DirectoryInfo dir in SubDirs)
            {
                ListViewItem item = new ListViewItem();
                item.Text = dir.Name;
                item.ImageIndex = 1;

                listView1.Items.Add(item);
            }

            foreach (FileInfo file in Files)
            {
                ListViewItem item = new ListViewItem();
                item.Text = file.Name;
                item.ImageIndex = 0;

                dateModified = File.GetLastWriteTime(file.FullName.ToString());
                item.SubItems.Add(dateModified.ToString());

                item.SubItems.Add((file.Length / 1024) + " KB");
                listView1.Items.Add(item);
            }

            listView1.EndUpdate();

        }

        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            pathtb.Text = e.Node.FullPath.ToString();
            ShowTreeViewItems(e.Node.FullPath.ToString());
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            treeView1.Nodes.Clear();
            DirectoryInfo CurrDir = new DirectoryInfo(comboBox1.SelectedItem.ToString());
            DirectoryInfo[] SubDir = CurrDir.GetDirectories();
            FileInfo[] Files = CurrDir.GetFiles();

            try
            {
                foreach (DirectoryInfo dir in SubDir)
                {
                    TreeNode node = treeView1.Nodes.Add(dir.Name);
                    node.Nodes.Add("");
                }
                foreach (FileInfo file in Files)
                {
                    if (file.Extension.ToLower() == ".txt")
                    {
                        TreeNode node = treeView1.Nodes.Add(file.Name);
                    }
                }
                
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            try
            {
                TreeNode currentNode = e.Node;
                DirectoryInfo FolderPath = new DirectoryInfo(comboBox1.SelectedItem + currentNode.FullPath);
                DirectoryInfo[] Folder = FolderPath.GetDirectories();
                FileInfo[] Files = FolderPath.GetFiles();

                foreach (DirectoryInfo dir in Folder)
                {
                    TreeNode node = currentNode.Nodes.Add(dir.Name);
                    node.Nodes.Add(" ");
                }

                foreach (FileInfo file in Files)
                {
                    if ((file.Extension).ToLower() == ".txt")
                    {
                        TreeNode newNode = currentNode.Nodes.Add(file.Name);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            comboBox1.Items.Add(e.Node.Text);
        }

        
    }
}





代码块添加 - OriginalGriff [/ edit]



[edit]Code block added - OriginalGriff[/edit]

推荐答案

直言不讳:我不明白你目前的设计,我没有时间把头放下去进入你的代码。但是,我想帮助你,我认为我能做到的最好的方法是向你展示我几年前开发的一些工作代码。



1使用Drive中的每个文件夹和子文件夹递归填充TreeView:



a。要求:使用System.IO;



b。假设:字符串变量'currentDrive



c中的有效驱动器名称。假定TreeView的treeView1和ListView的listView1。



d。 ListView有一列,View Property设置为'Details
To be blunt: I don't understand your current design, and I don't have the time to put my head into your code. But, I would like to assist you, and I think the best way I can do that is to show you some working code I developed a few years ago.

1. Filling a TreeView recursively with every folder and sub-folder in a Drive:

a. requires: using System.IO;

b. assumes: a valid Drive Name in a string variable 'currentDrive

c. assumes a TreeView 'treeView1, and a ListView 'listView1.

d. the ListView has one column, and the View Property is set to 'Details
// convenience variables
private string currentDrive = @"D://";

private DirectoryInfo currentDirectory;
private DirectoryInfo[] currentSubDirectories;

private TreeNode currentNode;

private void createTreeFromDrive()
{
    currentDirectory = new DirectoryInfo(currentDrive);
    currentSubDirectories = currentDirectory.GetDirectories();

    treeView1.Nodes.Clear();

    TreeNode currentNode = treeView1.Nodes.Add(currentDrive);

    fillTree(currentNode, currentSubDirectories);
}

// recursive code
private void fillTree(TreeNode currentNode, DirectoryInfo[] currentDirectories)
{
    foreach (var subDir in currentDirectories)
    {
        TreeNode subNode = currentNode.Nodes.Add(subDir.Name);

        // depending on the role/status of your User/App you may need more
        // or less work-arounds for no-go Directories
        if (subDir.Name.Contains("WindowsImageBackup")) continue;
        if (subDir.Name.Contains("System Volume Information")) continue;
        if (subDir.Name.Contains("Recycle")) continue;
        if (subDir.Name.Contains("Documents and Settings")) continue;
        if (subDir.Name.Contains("inetpub")) continue;

        DirectoryInfo[] subSubDirs = subDir.GetDirectories();
        
        if(subSubDirs.Length > 0) buildTree(subNode, subSubDirs);
    }
}

2。单击TreeNode,在ListView中显示相关目录中的文件:



a。假设您正在使用MS TreeView的默认路径分隔符字符串:反斜杠。

2. Given a click on a TreeNode, display the files in the associated Directory in a ListView:

a. assume you are using the default path separator string for a MS TreeView: the back-slash.

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    TreeNode currentNode = e.Node;

    listView1.Items.Clear();

    // requires .NET 4.0
    foreach (string file in Directory.EnumerateFiles(currentNode.FullPath, "*.*"))
    {
        listView1.Items.Add(file);
    }
}

评论:这个代码相当陈旧,除了使用.NET 4.0的一个修订版:如果我今天要重做它,我可能会改变一些事情。

Comments: this code is rather old, except for the one revision to use .NET 4.0: if I were to re-do it today, I'd probably change some things.


这篇关于如何制作树视图列表视图资源管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆