仅返回包含具有指定扩展名的文件的目录

时间:2016-11-01 00:37:49

标签: c# file recursion directory

当前功能:

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

此函数应返回具有指定扩展名的每个文件夹和文件。

目标:enter image description here

问题是它返回每个目录。如果我在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);
    }



}

2 个答案:

答案 0 :(得分:0)

为什么不首先制作整个文件夹结构的平面列表,包括文件,然后使用Linq对象。

对于类似

的列表
StringValue

如果需要,可以在TreeNode上添加父道具?

对于Linq而言

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

最后从列表中删除空目录

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

答案 1 :(得分:0)

要实现此目的,如果该文件夹没有任何目标文件,则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; } 应该能够返回null

TreeNode

您需要在null

时修改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; }
class GetNameAsync extends AsyncTask<String, String, JSONObject> {

JSONParser jsonParser = new JSONParser();
private static final String API_URL = "urlhere :-)";
private static final String TAG_NAMES = "names";
private ProgressDialog pDialog;

@Override
protected void onPreExecute() {
    pDialog = new ProgressDialog(getActivity());
    pDialog.setMessage("Attempting loading...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(true);
    pDialog.show();
}

@Override
protected JSONObject doInBackground(String... args) {
    try {
        HashMap<String, String> params = new HashMap<>();
        params.put("access", KEY);
        params.put("lang", LANG);

        Log.e("request", "starting");
        JSONObject names_json = jsonParser.makeHttpRequest(API_URL, "GET", params);
        return names_json;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
@Override
protected void onPostExecute(JSONObject names_json) {
    super.onPostExecute(names_json);
    if (pDialog != null && pDialog.isShowing()) {
        pDialog.dismiss();
    }
    String names_entry = "";
    String mAktienlisteAdapter1 = "";

    try {
        names_entry = names_json.getString(TAG_NAMES);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    getFinalResult(String.valueOf(names_entry)); // Here the value has been updated and you can Call you next business depends on the results

}
}

所以基本上,它包含所有文件夹,如果它包含目标文件或文件夹包含目标文件。