0

我在 C# 中创建一个窗口窗体,我有一个TreeView控件。我必须执行以下任务:

  • 单击浏览按钮
  • 选择基础文件夹
  • 显示基本文件夹的所有文件(.doc/ .docx)和文件夹及其图标
  • 单击子文件夹将其展开并显示其中包含的文件和文件夹

请给我一些建议/链接。

4

2 回答 2

0

我使用了以下代码:

   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);
                        }
                    }
                }
            }
        }
于 2013-01-09T14:04:43.340 回答
0

这必须帮助你http://www.csharp-examples.net/get-files-from-directory/

于 2012-09-22T11:29:27.117 回答