Why is this function returning nothing, although there is a match?

这一生的挚爱 提交于 2019-12-02 10:20:16

In your recursion, you are losing the files you find in the subdirectories. Capture the return filenames this way:

foreach (string d in Directory.GetDirectories(dirName))
{
    fileNames.AddRange(GetXMLFiles(fileType, d));
}

What is going on is that this line var fileNames = new List<String>(); creates a local variable called fileNames. You may think because your method is static, the variables inside the method are static. This is not the case. So, each time you call GetXMLFiles, a copy of this variable is created for each call.

Since fileNames is local to each call of GetXMLFiles, you need to return to the caller all the fileNames it finds and the caller needs to add those to the collection that is local to it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!