【问题标题】:Processing of the results of the recursive search of subdirectories and files C#递归搜索子目录和文件的结果的处理C#
【发布时间】:2015-10-23 18:14:38
【问题描述】:

任务是为所选目录实现子目录和文件的递归枚举。 4个线程: 第一个 - 主要(Application.Run()); 2nd - 扫描所选目录并将文件写入一些公开可用的集合(我不明白在哪个集合中); 3rd - 从第二个线程接收有关子目录或来自“公共可用集合”的文件的信息,并将结果存储在 xml 文件中(异步)。保持目录的层次结构; 4th - 从第二个线程接收来自“公共可用集合”的子目录或文件的信息,并将结果存储在 treeView 中(异步)。保持目录的层次结构; 有一种方法可以执行递归搜索。

private void WalkDirectoryTree(System.IO.DirectoryInfo root)
    {
        System.IO.FileInfo[] files = null;
        System.IO.DirectoryInfo[] subDirs = null;

        try
        {
            files = root.GetFiles("*.*");
        }           
        // than the application provides. 
        catch (UnauthorizedAccessException e)
        {
            log.Add(e.Message);
        }

        catch (System.IO.DirectoryNotFoundException e)
        {
            Console.WriteLine(e.Message);
        }

        if (files != null)
        {
            if (count > 0)
            {
//i have introduced variable: count = 0 for understanding, in which 
//direction are we moving through the hierarchy: top or down
                tn[count] = tn[count - 1].Nodes.Add(root.Name.ToString() + " " + count);
                list.Add(new CustomClass(root.Name, count));
            }

            //textBox1.AppendText("Directory: "+root.Name+"\n");
            count++;
            foreach (System.IO.FileInfo fi in files)
            {
                //textBox1.AppendText(fi.Name +fi.CreationTime+fi.Length+ "\n");
                list.Add(new CustomClass(fi.Name,count));
                tn[count] = tn[count - 1].Nodes.Add(fi.Name +" " + count);
            }
            subDirs = root.GetDirectories();
            foreach (System.IO.DirectoryInfo dirInfo in subDirs)
            {
                WalkDirectoryTree(dirInfo);
                count--;
            }               
        }
    }

我也有我堆叠的方法:存储结果的方法(子目录或文件到 xml 文件)

private void WriteToXML()
    {
        textBox1.Clear();
        doc = new XDocument(new XElement("Folder","Oreh"));
// root element is created manually, because i can't implement corrctly
// this method even in this way!
        int newcount = 1;
//newcount is a variable with the help of which i can define the direction
//of moving though the hierarchy by comparing with l.ParentID(count in prev. method)
        xElem[newcount] = doc.Root;
        foreach (var l in list)
        {
            if (l.ParentID > newcount)
            {
            xElem[newcount +1] = new XElement("Name", l.Name,new Attribute("ID",l.ParentID));
            xElem[newcount].Add(xElem[newcount+ 1]);
            newcount=l.ParentID;
            }
            else if (l.ParentID < newcount)
            {
            newcount --;
            xElem[newcount - 1] = new XElement("Name", l.Name, new XAttribute("ID", l.ParentID));
            xElem[newcount].Add(xElem[newcount - 1]);
            }
            else
            {
            xElem[newcount].Add(new XElement("Name", l.Name, new XAttribute("ID", l.ParentID)));
            }
        }
        doc.Save("Tree.xml");
    }

问题是:我不知道从WalkDirectoryTree()方法获得的结果在哪个集合中以及我应该如何存储结果以及如何将这个结果写入xml文件。

【问题讨论】:

  • 好吧。你似乎有很多东西正在发生.. list 似乎是你在两者中使用的全局.. 所以写出 xml 的明显答案是一旦你完成了对文件夹的迭代.. count 似乎也是全局的使用它似乎也是一种奇怪的方式,当您可以传递目录深度并仅在向下时递增.. 如果您没有使用这么多全局变量,您可以进行并行处理
  • 我只需要用线程来实现它。但首先,我需要弄清楚如何将找到的子目录或文件的信息存储到集合中(我认为我不能在 List 的帮助下做到这一点),并将其发送到将其添加到 xml 的方法节点(保存层次结构)和形成树视图的方法。集合必须是公开的(由多个线程访问)
  • 您的代码将其全部整合到一个列表中。它不会为每个子目录执行此操作,因此,您必须在最后完成所有操作,否则您的数据将没有意义。
  • 那么我怎么能把它作为子目录呢?如何在这些节点之间建立连接?我在变量计数的帮助下完成了它(我已经从该方法构建了treview,它工作正常),但它似乎是错误的方式。如何在集合的帮助下将每个子目录和文件附加到其父目录?这几乎是我的主要问题。

标签: c# multithreading recursion treeview


【解决方案1】:

我会实现一个类来简化事情和信息。这是一个例子。您需要阅读如何管理目录中的空父目录或零目录。考虑一下这是某种伪代码

class DirInfo
{
public string ParentDir { get; set; };
public string DirName { get; set; };
public string[] files { get; set; };
public DirectoryInfo[] Dirs = null { get; set; };
}

List<DirInfo> Directories = new List<DirInfo>(); 
//We don't really know the amount
//unless we iterate through it, so you can make an array []

private void DirSearch(System.IO.DirectoryInfo Root)
{
//You need to add you own validations when parent is null, or not subdirectories inside or 0 files inside, etc.
Directories.Add(new DirInfo() { ParentDir = Root.Parent.Name, DirName = Root.Name, files = Root.GetFiles("*.*"), Dirs = Root.GetDirectories() };

if(Root.Dirs != null)
for(int i=0; i<Root.Dirs.Lenght; i++){

DirSearch(Root.Dirs[i]);

}

}

在此之后,您将获得一个包含目录信息的列表,例如:

RootDir { Parent: N/A -> Name: RootDir -> Files: ... File1, FIle2... -> SubDirs: Dir1... Dir2... }

Dir1 { Parent: RootDir -> Name: Dir1 -> Files... ... -> SubDirs: DirSub1, DirSub2... }

DirSub1 { Parent: Dir1 -> Name: DirSub1 -> Files... ... -> SubDirs: Etc, Etc... }

现在,您可以通过对它们进行排序来创建一个目录数组...您可以选择其父目录为 RootDir、Dir1 等的每个目录...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 2012-04-07
    • 2014-07-07
    • 1970-01-01
    • 2021-05-25
    • 2013-08-28
    相关资源
    最近更新 更多