Current Function:
public static TreeNode GetFolderStructure(string path, List<string> allExt)
{
TreeNode result = new TreeNode(path, "DIR");
foreach (string dirName in Directory.GetDirectories(path))
{
result.Append(GetFolderStructure(dirName, allExt));
}
foreach (string item in allExt)
{
foreach (string fileName in Directory.GetFiles(path, item))
{
result.Append(fileName, "FILE");
}
}
return result;
}
This function should return every Folder(s) and File(s) with specified extension.
The problem is that it returns every directory. If I add the path below the foreach I get a unassigned local variable which creates every time a exception...
My TreeNode Class:
class TreeNode
{
private List<TreeNode> childNodes = new List<TreeNode>();
public IList<TreeNode> ChildNodes { get { return childNodes.AsReadOnly(); } }
public string Value { get; private set; }
public string ValueType { get; private set; }
public TreeNode(string newValue, string newValueType)
{
Value = newValue;
ValueType = newValueType;
}
public TreeNode Append(TreeNode newNode)
{
if (newNode == null || childNodes.Contains(newNode))
throw new Exception("File/Folder does not excist OR the File/Folder is already in the List");
childNodes.Add(newNode);
return newNode;
}
public TreeNode Append(string newValue, string newValueType)
{
TreeNode newNode = new TreeNode(newValue, newValueType);
return Append(newNode);
}
}