0

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.

Goal:enter image description here

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);
    }



}
  • Please show your exception. Moreover, please study the [TreeNode](https://msdn.microsoft.com/zh-tw/library/12bxet86(v=vs.110).aspx) first, you should use `result.ChildNodes.Add`, and I doubt that `TreeNode` is incorrect syntax. – Prisoner Nov 01 '16 at 01:09
  • Should separate file/folder processing from UI elements. – Lei Yang Nov 01 '16 at 02:08
  • @Alex I can not run the program because there is a unassigned local variable. I get an error.. I don't use result.ChildNodes.Add because I have a recursive function so instead writing result.ChildNodes.Add i say result.Append(...); –  Nov 01 '16 at 02:08
  • Seems my mistake in my last comment, I'm asking `TreeNode.Append`, I can't find anything in MSDN, could you? – Prisoner Nov 01 '16 at 02:14
  • Anyway, this should be what you need: [Populate TreeView with file system directory structure](http://stackoverflow.com/a/6239644/1050927) – Prisoner Nov 01 '16 at 02:16
  • @Alex TreeNode is Class written by me not from MSDN.. –  Nov 01 '16 at 02:17
  • Surprise~~~, then please post your `TreeNode`, otherwise, we cannot help you at all – Prisoner Nov 01 '16 at 02:18
  • Posted. I hope it can help.. –  Nov 01 '16 at 02:23
  • Can you point out which line have exception? I tried your class, but run without error. – Prisoner Nov 01 '16 at 02:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/127107/discussion-between-lebron11-and-alex). –  Nov 01 '16 at 12:31
  • Yes It runs without an error, but that is not the function I want. I want a function where specified directories are saved not every directory(which is in my current function).... –  Nov 01 '16 at 12:33

2 Answers2

0

Why not make a flat list of the whole folder structure first including the files and then use Linq to objects.

For the list something like

IList<TreeNode> flatList = new List<TreeNode>()

Maybe add a parent prop on your TreeNode if needed?

And for the Linq something like

flatList = flatList.Where(tn => tn.Type.Equals("DIR") || allExt.Contains(tn.FileExt)).ToList();

And finally removing Empty dirs from the list

flatList.RemoveAll(tn => tn.Type.Equals("DIR") && !flatList.Any(ftn => ftn.Type.Equals("FILE") && ftn.Parent.Equals(tn.Path)));
Henry Roeland
  • 492
  • 5
  • 19
0

To achieve this, your GetFolderStructure should able to return null if that folder do not have any target files

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");
        }
    }

    if (result.ChildNodes.Count > 0) // <- check do it have any child
        return result;
    else                             // if not, return null, so it will not include in result
        return null;
}

And you need to modify your TreeNode to accept null when Append

public TreeNode Append(TreeNode newNode)
{
    // I have change to return this, I think you want to have fluent design
    // change to other thing if you are not
    // and this line will check if newNode is null, do nothing
    if (newNode == null) return this;

    if (childNodes.Contains(newNode))
        throw new Exception("the File/Folder is already in the List");

    childNodes.Add(newNode);
    return this;
}

public TreeNode Append(string newValue, string newValueType)
{
    TreeNode newNode = new TreeNode(newValue, newValueType);
    return Append(newNode);
}

// I have add this in order to test the program, you can remove it
public string ToString(string prefix)
{
    string result = string.Format("{0}{1}: {2}\r\n", prefix, ValueType, Value);
    foreach (var item in childNodes)
    {
        result += item.ToString(prefix + "\t");
    }
    return result;
}

So basically, it will include all folders if it contain target file(s) or the folder contains target file(s).

Prisoner
  • 1,839
  • 2
  • 22
  • 38