Treeview检查文件和文件夹在C#中加载ListView [英] Treeview checked files and folders load in ListView in C#

查看:136
本文介绍了Treeview检查文件和文件夹在C#中加载ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用树视图以递归方式加载文件和文件夹。它需要在30秒内加载2GB文件和文件夹,但是当treeview检查文件和文件夹以递归方式加载到listview中时。这需要20分钟(这是一个巨大的时间)。请给出一个算法如何解决这个问题,以便更快地计算。



我的源代码如下....



I have used a treeview to load files and folders in recursive way. It requires to load 2GB files and folder within 30 seconds, but when treeview checked files and folders load in listview in recursive way. It''s require 20 minites(that is huge time). Please give a algorithm how to solve this problem to faster calculation.

My source code are given below....

private void btnAddToListViewCheckedItem_Click(object sender, EventArgs e)
        {
            foreach (TreeNode tn in tvLoadTreeviewFromListView.Nodes)
            {
                
                AddToListView(tn);
            }
        }

        private void AddToListView(TreeNode tn)
        {
        
            {
                
                FileInfo file = new FileInfo(tn.Text);

                string[] suffix = { "B", "KB", "MB", "GB", "TB" };
                if (file.Exists)
                {
                    int s = 0;
                    long size = file.Length;
                    while (size >= 1024)
                    {
                        NO_OF_FILES += 1;
                        s++;
                        size /= 1024;
                    }
                    


                    String countFileSize = string.Format("{0}{1}", size, suffix[s]);

                   
                    ListViewItem item = lvLoadSelectedFileAndFolder.Items.Add(file.Name);

                    item.ImageIndex= sysIcons.GetIconIndex(tn.Text);
                    item.SubItems.Add(tn.Text);
                    item.SubItems.Add(countFileSize);
                  
                }

                foreach (TreeNode tnx in tn.Nodes)
                {
                   

                    for (int i = 0; i < lvLoadSelectedFileAndFolder.Items.Count; i++)
                    {
                        int ii = 1;
                        if (tnx.Text == lvLoadSelectedFileAndFolder.Items[i].SubItems[ii].Text)
                        {
                            return;

                        }
                    }


                    if (tnx.Checked)
                    {
                       
                        AddToListView(tnx);
                    }

                }
            }
            catch (Exception ){}

        }

推荐答案

您好,



我建议您使用延迟加载这意味着你只需加载1个或几个级别而不是整个树。



这样它加载速度很快。您可以在背景上加载第二级,以便用户不会感到任何延迟。



这是大树中最常用的方法。



*同样的技术用于通过网络浏览器和幻灯片等加载图像。





干杯,

Edo
Hello,

I would suggest that you use "Lazy Loading" which means you load only 1 or several levels instead of the whole tree right up front.

This way it loads quickly. You can load the second level on the background so that the user does not feel any delay.

This is a most common approach in large trees.

* The same technique is used for loading images by web-browsers and for slide shows etc.


Cheers,
Edo


这篇关于Treeview检查文件和文件夹在C#中加载ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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