简体   繁体   English

如何遍历物理文件夹并读取C#中的叶文件

[英]How to iterate through the physical folders and read a leaf file in C#

I have a root node A which contains B which contains C which contains D which contains an XML file abc.xml So in D:\\ drive ,I have the following structure of directories A>>B>>C>>D. 我有一个根节点A,其中的B包含C,C包含C,D包含一个XML文件abc.xml因此,在D:\\ drive中,我具有目录A >> B >> C >> D的以下结构。 This path is dynamic. 此路径是动态的。 What is the best practice to read the file abc.xml in C# by iterating through the physical folders? 通过遍历物理文件夹读取C#中的abc.xml文件的最佳实践是什么?

You could implement a recursive search algorithm that goes through all the folders and descends into the sub folders. 您可以实现一个递归搜索算法,该算法遍历所有文件夹,然后进入子文件夹。

Pseudo Code: 伪代码:

public void GetXMLFilesRecursive(string currentFolder, List<string> results)
{
    // Enumerate all directories of currentFolder
    string[] folders = Directory.GetDirectories(currentFolder);
    foreach (string folder in folders)
        GetXMLFilesRecursive(folder, results));

    // Enumerate all XML files in this folder only if it has no other sub-folders (is a leaf)
    if (folders.Length == 0)
    {
        string[] xmlFiles = Directory.GetFiles(currentFolder, "*.xml");
        results.AddRange(xmlFiles);
    }
}

This method only returns XML files in the lowest folders of the hierarchy (ie folders that don't have sub folders). 此方法仅在层次结构的最低文件夹(即没有子文件夹的文件夹)中返回XML文件。 If you want all files you find along the way, comment out if (folders.Length == 0) . 如果要查找所有文件,请注释if (folders.Length == 0) On the other hand, you could then also use Directory.GetFiles with SearchOption.AllDirectories . 另一方面,您还可以将Directory.GetFilesSearchOption.AllDirectories一起SearchOption.AllDirectories


Why I wrote a recursive algorithm: The OP asked how to find all XML files in the leaf directories. 我为什么要编写递归算法:OP询问如何在叶子目录中找到所有XML文件。 You can not do that using Directory.GetFiles with SearchOption.AllDirectories , but you then need to implement the above. 您不能使用带有SearchOption.AllDirectories Directory.GetFiles来执行此SearchOption.AllDirectories ,但是您需要实现以上内容。

您可以使用Directory.GetFiles(d, "*.xml",SearchOption.AllDirectories)获取所有xml文件,以获取所需的内容。

You can search an entire tree for a file using Directory.GetFiles(path,searchPattern,SearchOption) or Directory.EnumerateFiles with SearchOption.AllDirectories , eg 您可以使用Directory.GetFiles(path,searchPattern,SearchOption)Directory.EnumerateFilesSearchOption.AllDirectories搜索整个树中的文件,例如

var fullPaths=Directory.GetFiles(myPath,"abc.xml",SearchOption.AllDirectories)

You can also use the DirectoryInfo class to get full FileInfo instances instead of just the paths, with access to file properties and attributes: 您还可以使用DirectoryInfo类来获取完整的FileInfo实例,而不仅仅是路径,从而可以访问文件属性和属性:

var myDir=new DirectoryInfo(myPath);
var fileInfos=myDir.GetFiles("abc.xml",SearchOption.AllDirectories);

The difference between the GetFiles and EnumerateFiles methods is that the first returns an array with all the files found, blocking until it finds all of them. GetFiles和EnumerateFiles方法之间的区别在于,第一个返回一个包含找到的所有文件的数组,直到找到所有文件为止,它一直阻塞。 EnumerateFiles on the other hand returns results as it finds them, so you get to process the results much sooner. 另一方面, EnumerateFiles在找到结果时会返回结果,因此您可以更快地处理结果。

What goes for GetFiles goes for the GetDirectories / EnumerateDirectories set of functions as well. GetFiles也适用于GetDirectories / EnumerateDirectories函数集。 The methods are available both from the Directory and DirectoryInfo class. 该方法可从Directory和DirectoryInfo类获得。

If you want to search for both directories and files, you can use GetFileSystemEntries / EnumerateFileSystemEntries to return both of them with a single call. 如果要同时搜索目录和文件,则可以使用GetFileSystemEntries / EnumerateFileSystemEntries通过一次调用将它们都返回。 The equivalent DirectoryInfo methods are GetFileSystemInfos / EnumerateFileSystemInfos 等效的DirectoryInfo方法是GetFileSystemInfos / EnumerateFileSystemInfos

public List<string> getFiles(string path, string searchPattern, List<string> list)
{
    try
    {
        foreach (string folder in Directory.GetDirectories(path))
            getFiles(folder, searchPattern, list);
        list.AddRange(Directory.GetFiles(path, searchPattern));
    }
    catch (UnauthorizedAccessException)
    {
        //Do not have access to the file.
    }
    return list;
}

Call like this: 像这样打电话:

//Get all xml files in the D drive:
List<string> files = getFiles(@"d:\", "*.xml", new List<string>());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM