显示文件夹,并在树视图与他们的图标文件(.DOC只/ DOCX) [英] Display folder and file (only .doc/docx) in tree view with their icon

查看:97
本文介绍了显示文件夹,并在树视图与他们的图标文件(.DOC只/ DOCX)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建在C#中的窗口形式在那里我有一个 TreeView控件控制。我要做以下任务:

I am creating a window form in C# where I have a TreeView control. I have to do following task:


  • 单击在浏览按钮

  • 选择基本文件夹

  • 显示所有文件( .DOC / .DOCX ),并与他们的图标基本文件夹的文件夹

  • 点击子文件夹将其展开,并显示其包含的文件和文件夹

  • Click on a browse button
  • Select the base folder
  • Display all the files(.doc/.docx) and folder of the base folder with their icon
  • Click the sub-folder to expand it and display the files and folders it contains

请给我一些建议/链接。

Kindly give me some suggestions/links.

推荐答案

我已经使用泰德以下code:

I have used thet following code:

   void GetTree(string strSearchPath)
        {
            treeFiles.Nodes.Clear();
            SetNode(treeFiles, strSearchPath);
            treeFiles.TopNode.Expand();
        }

        void SetNode(TreeView treeName, string path)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(path);
            TreeNode node = new TreeNode(dirInfo.Name);
            node.Tag = dirInfo;
            GetFolders(dirInfo, node);
            GetFiles(dirInfo, node);
            treeName.Nodes.Add(node);
        }

        void GetFolders(DirectoryInfo d, TreeNode node)
        {

            try
            {
                DirectoryInfo[] dInfo = d.GetDirectories();

                if (dInfo.Length > 0)
                {
                    TreeNode treeNode = new TreeNode();
                    foreach (DirectoryInfo driSub in dInfo)
                    {
                        treeNode = node.Nodes.Add(driSub.Name, driSub.Name, 0, 0);
                        GetFiles(driSub, treeNode);
                        GetFolders(driSub, treeNode);
                    }
                }
            }
            catch (Exception ex) { }

        }

        void GetFiles(DirectoryInfo d, TreeNode node)
        {
            var files = d.GetFiles("*.doc*");
            FileInfo[] subfileInfo = files.ToArray<FileInfo>();

            if (subfileInfo.Length > 0)
            {
                for (int j = 0; j < subfileInfo.Length; j++)
                {
                    bool isHidden = ((File.GetAttributes(subfileInfo[j].FullName) & FileAttributes.Hidden) == FileAttributes.Hidden);
                    if (!isHidden)
                    {
                        string strExtention = Path.GetExtension(subfileInfo[j].FullName);
                        if (strExtention.Contains("doc"))
                        {
                            TreeNode treeNode = new TreeNode();
                            string path = subfileInfo[j].FullName;
                            string name = subfileInfo[j].Name;
                            treeNode = node.Nodes.Add(path, name, 1, 1);
                            AddBookMarkFile(path, treeNode);
                        }
                    }
                }
            }
        }

这篇关于显示文件夹,并在树视图与他们的图标文件(.DOC只/ DOCX)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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