0

我有结构:

public struct BaseFile
{
    public string FileName;
    public string Path; // this is not the full path. it is the fullPath of it's parent directory in other words.
}

我有课

 public class MyFileDir // can be a file or directory
 {
      public string Name;
      public string FullPath;
      public List<MyFileDir> Children;
      public bool IsDirectory;
      // many more other properties
 }

所以我有一个线程放置文件LinkedList<BaseFile> myLinkedList ,我希望另一个线程也开始投射这些文件MyFileDir root(注意我使用linkedList而不是list,因为linkedList的地址不会改变List每次需要增长时都会改变它的地址)

我有一个布尔变量 IsRunning,它会让我知道其他线程是否仍在将基本文件添加到链接列表中。所以我有类似的东西:

        var curNode = myLinkedList.First;

        while (IsRunning)
        {
            if (curNode.Next == null) // wait until next node is not null
            {
                Thread.Sleep(100);
                continue;
            }

            curNode = curNode.Next;
            AddItemToTree(curNode.Value);
        }

如您所见,我在实现该方法时遇到了麻烦,AddItemToTree我基本上想开始以该方法构建树。所以首先我将查看根目录并搜索我应该添加的目录curNode.Value。我很难做到这一点。

4

1 回答 1

1
     MyFileDir root = new MyFileDir(); //root Node

&

     var curNode = myLinkedList.First;

    while (IsRunning)
    {
        if (curNode.Next == null) // wait until next node is not null
        {
            Thread.Sleep(100);
            continue;
        }

        curNode = curNode.Next;
        curFileDir = new MyFileDir(curNode);// your cast here


    List<string> mylist = curFileDir.FullPath.Split(new string[] { @"\" }, StringSplitOptions.RemoveEmptyEntries).ToList(); // this gives a list of dirs in FullPath to explore



        MyFileDir temp = root;
        foreach (string filedirName in mylist)
        {
            temp = AddNode(temp, filedirName );
        }


  }

第一个循环意味着如果节点存在则返回它,否则创建它并返回它

       private MyFileDir AddNode(MyFileDir parent, string filedirName)
{
    foreach (MyFileDir subfiledir in parent.Children)
        if (subfiledir.Name == header)
            return subfiledir;

    MyFileDir filedir = new MyFileDir(fileName);
    parent.Children.Add(fileName);
    return fileName;
}
于 2012-09-19T23:12:20.820 回答