递归搜索目录中的文件 [英] Searching for file in directories recursively

查看:33
本文介绍了递归搜索目录中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码通过目录递归搜索文件,该目录将所有 xml 文件的列表返回给我.一切正常,只是根目录中的 xml 文件未包含在列表中.

I have the following code to recursively search for files through a directory, which returns a list of all xml files to me. All works well, except that xml files in the root directory are not included in the list.

我明白为什么,因为它做的第一件事是获取根目录中的目录,然后获取文件,因此错过了根上的 GetFiles() 调用.我尝试在 foreach 之前包含 GetFiles() 调用,但结果与我预期的不同.

I understand why, since the first thing it does is get the directories in the root, then get files, thus missing the GetFiles() call on the root. I tried including the GetFiles() call prior to the foreach, but the results are not as I expect.

public static ArrayList DirSearch(string sDir)
{
    try
    {
        foreach (string d in Directory.GetDirectories(sDir))
        {
            foreach (string f in Directory.GetFiles(d, "*.xml"))
            {
                string extension = Path.GetExtension(f);
                if (extension != null && (extension.Equals(".xml")))
                {
                fileList.Add(f);
                }
            }
            DirSearch(d);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    return fileList;
}

我的目录结构类似于:

RootDirectory
        test1.0.xml
            test1.1.xml
            test1.2.xml
  2ndLevDir
            test2.0.xml
            test2.1.xml
  3rdLevDir
               test3.0.xml
               test3.1.xml

代码返回:

test2.0.xml
test2.1.xml
test3.0.xml
test3.1.xml

我想返回每个文件,包括:

I would like to return every file including:

test1.0.xml
test1.1.xml
test1.2.xml

不是很好的递归诗句.任何指针将不胜感激.

Not very well verse with recursion. Any pointers would be greatly appreciated.

推荐答案

您可以使用 Directory.GetFiles 的重载 为您搜索子目录,例如:

You could use this overload of Directory.GetFiles which searches subdirectories for you, for example:

string[] files = Directory.GetFiles(sDir, "*.xml", SearchOption.AllDirectories);

只能搜索一个扩展名,但您可以使用以下内容:

Only one extension can be searched for like that, but you could use something like:

var extensions = new List<string> { ".txt", ".xml" };
string[] files = Directory.GetFiles(sDir, "*.*", SearchOption.AllDirectories)
                    .Where(f => extensions.IndexOf(Path.GetExtension(f)) >= 0).ToArray();

选择具有所需扩展名的文件(注意,扩展名区分大小写).

to select files with the required extensions (N.B. that is case-sensitive for the extension).

在某些情况下,可能需要使用 Directory.EnumerateFiles 方法:

In some cases it can be desirable to enumerate over the files with the Directory.EnumerateFiles Method:

foreach(string f in Directory.EnumerateFiles(sDir, "*.xml", SearchOption.AllDirectories))
{
    // do something
}

如果代码在没有适当访问权限的帐户下运行,请查阅文档以了解可能引发的异常,例如 UnauthorizedAccessException.

Consult the documentation for exceptions which can be thrown, such as UnauthorizedAccessException if the code is running under an account which does not have appropriate access permissions.

如果 UnauthorizedAccessException 是一个问题,那么请在 Directory.EnumerateFiles => 上查看正确的答案.UnauthorizedAccessException.

If the UnauthorizedAccessException is a problem, then please see the fine answers at Directory.EnumerateFiles => UnauthorizedAccessException.

这篇关于递归搜索目录中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆