获取递归方法的结果列表

时间:2013-08-11 02:52:42

标签: c# winforms recursion

我有这段代码:

private void SearchForDoc()
{
     try
     {
        outputtext = @"c:\temp\outputtxt";
        outputphotos = @"c:\temp\outputphotos";
        temptxt = @"c:\temp\txtfiles";
        tempphotos = @"c:\temp\photosfiles";
        if (!Directory.Exists(temptxt))
        {
            Directory.CreateDirectory(temptxt);
        }
        if (!Directory.Exists(tempphotos))
        {
            Directory.CreateDirectory(tempphotos);
        }
        if (!Directory.Exists(outputtext))
        {
            Directory.CreateDirectory(outputtext);
        }
        if (!Directory.Exists(outputphotos))
        {
            Directory.CreateDirectory(outputphotos);
        }
        t = Environment.GetEnvironmentVariable(Environment.GetFolderPath(Environment.SpecialFolder.Personal));

        ApplyAllFiles(t, ProcessFile);
        for (int i = 0; i < textfiles.Length; i++)
        {
            FileInfo fi = new FileInfo((textfiles[i]));
            DirectoryInfo d = new DirectoryInfo(temptxt);
            long dirSize = DirSize(d);

            if ((dirSize + fi.Length) <= 8388608)
                fi.CopyTo(temptxt + "\\" + fi.Name, true);
            else
                break;
        }
        Compressions("textfiles.zip", temptxt, outputtext);

问题出在这一部分:

for (int i = 0; i < textfiles.Length; i++)

textfilesstring[]但现在textfiles已不再存在,因为我使用的是ApplyAllFiles方法:

static void ProcessFile(string path) {/* ... */}

static void ApplyAllFiles(string folder, Action<string> fileAction)
{
    foreach (string file in Directory.GetFiles(folder))
    {
        fileAction(file);
    }
    foreach (string subDir in Directory.GetDirectories(folder))
    {
        try
        {
            ApplyAllFiles(subDir, fileAction);
        }
        catch
        {
            // swallow, log, whatever
        }
    }
}

编辑**

我的方法现在我有:

string t = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string[] textfiles = ApplyAllFiles(t, "*.txt", ProcessFile).ToArray();

然后是ApplyAllFiles方法:

static void ProcessFile(string path) {/* ... */}
        static IEnumerable<string> ApplyAllFiles(string folder, string searchPattern, Action<string> fileAction)
        {
            foreach (string file in Directory.GetFiles(folder))
            {
                fileAction(file);
                yield return file;
            }
            foreach (string subDir in Directory.GetDirectories(folder, searchPattern))
            {
               // try
                //{
                    foreach (string file in ApplyAllFiles(subDir, searchPattern, fileAction))
                    {
                        yield return file;
                    }
               // }
               // catch
               // {
                    // swallow, log, whatever
               // }
            }
        }

不得不删除尝试捕获产量不能尝试并捕获 其次我在第二个内部foreach循环中添加了searchPatterns变量

结果是我只得到pdf或某些文件不是关于25个文件的文本而只是来自主dosuments目录。

1 个答案:

答案 0 :(得分:1)

您可以获取ApplyAllFiles处理的所有文件的列表,如下所示:

static IEnumerable<string> ApplyAllFiles(string folder, Action<string> fileAction)
{
    IEnumerable<string> files = Directory.GetFiles(folder);
    foreach (string file in files)
    {
        fileAction(file);
    }

    foreach (string subDir in Directory.GetDirectories(folder))
    {
        try
        {
            files = files.Concat(ApplyAllFiles(subDir, fileAction));
        }
        catch
        {
            // swallow, log, whatever
        }
    }

    return files;
}

但是这会返回一个IEnumerable<string>将其变成string[]使用它:

var textfiles = ApplyAllFiles(t, ProcessFile).ToArray();

要仅将指定的操作应用于某些文件,您可以传入一个新参数,如下所示:

static IEnumerable<string> ApplyAllFiles(string folder, string searchPattern, Action<string> fileAction)
{
    IEnumerable<string> files = Directory.GetFiles(folder, searchPattern);
    foreach (string file in files)
    {
        fileAction(file);
    }

    foreach (string subDir in Directory.GetDirectories(folder))
    {
        try
        {
            files = files.Concat(ApplyAllFiles(subDir, searchPattern, fileAction));
        }
        catch
        {
            // swallow, log, whatever
        }
    }

    return files;
}

var textfiles = ApplyAllFiles(t, "*.txt", ProcessFile).ToArray();

但请注意,已有method允许您以递归方式搜索目录。整个结构可以简化为:

var textfiles = Directory.GetFiles(t, "*.txt", SearchOption.AllDirectories);
foreach(var f in textfiles)
{
    ProcessFile(f);
}