簡體   English   中英

僅返回包含具有指定擴展名的文件的目錄

[英]Return only directories which contain files with the specified extension

當前功能:

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

此函數應返回具有指定擴展名的每個Folder(文件夾)和File(文件)。

目標: 在此處輸入圖片說明

問題是它返回每個目錄。 如果我在foreach下面添加路徑,則會得到一個未分配的局部變量,該變量每次都會創建一個異常...

我的TreeNode類:

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



}

為什么不首先列出整個文件夾結構的平面列表,包括文件,然后對對象使用Linq。

對於列表類似

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

如果需要,也許在您的TreeNode上添加一個父道具?

對於Linq來說,

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

最后從列表中刪除Empty dirs

flatList.RemoveAll(tn => tn.Type.Equals("DIR") && !flatList.Any(ftn => ftn.Type.Equals("FILE") && ftn.Parent.Equals(tn.Path)));

為此,如果該文件夾沒有任何目標文件,則GetFolderStructure應該能夠返回null。

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

並且您需要修改TreeNode以在Append接受null

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

因此,基本上,它將包含所有包含目標文件或包含目標文件的文件夾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM