I have the struct:
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.
}
and I have the class
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
}
So I have one thread place files on LinkedList<BaseFile> myLinkedList
and I will like another thread to start casting those files too MyFileDir root
(note I used linkedList instead of list because the addresses of linkedList does not change where a List changes its address every time it needs to grow)
I have a bool variable IsRunning that will let me know if the other thread is still adding base files to the linked list. So I have something like:
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);
}
so as you can see I am having trouble implementing the method AddItemToTree
I basically will like to start building the tree in that method. So first I will look at the root and search the directory where I should add curNode.Value
. I am having a hard time doing that.
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 );
}
}
the first loop means if the node exists return it, else create it & return it
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;
}