3

我需要计算在这个递归函数中删除的文件数。由于它是递归的,我不能使用if语句,而且 C# 不支持全局变量。有什么选择吗?

static void DirSearch(string path)
{
    try
    {
        foreach (string dirPath in Directory.GetDirectories(path))
        {
            foreach (string filePath in Directory.GetFiles(dirPath))
            {
                string filename = Path.GetFileName(filePath);
                if (filename.Equals("desktop.txt"))
                {
                    File.Delete(filePath);
                    //count++
                }
                Console.WriteLine(filePath); // print files
            }
            Console.WriteLine(dirPath); // print directories
            DirSearch(dirPath);
        }
    }
    catch (System.Exception excpt)
    {
        Console.WriteLine(excpt.Message);
    }
}
4

4 回答 4

14

一种方法是传入一些东西让它算进去。我会这样做ref,例如:

static void DirSearch(string path, ref int count)
{
    try
    {
        foreach (string dirPath in Directory.GetDirectories(path))
        {
            foreach (string filePath in Directory.GetFiles(dirPath))
            {
                string filename = Path.GetFileName(filePath);
                if (filename.Equals("desktop.txt"))
                {
                    File.Delete(filePath);
                    count++
                }
                Console.WriteLine(filePath); // print files
            }
            Console.WriteLine(dirPath); // print directories
            DirSearch(dirPath,ref count);
        }
    }
    catch (System.Exception excpt)
    {
        Console.WriteLine(excpt.Message);
    }
}

然后调用它:

int count = 0;

DirSearch(@"C:\SomePath",ref count);

然后,您可以count像在代码中注释掉的那样正常使用。

于 2013-11-11T18:43:08.147 回答
8

count尝试如下递归。因此DirSearch返回count已删除文件的。

static int DirSearch(string path)
{
    int count = 0;

    try
    {
        foreach (string dirPath in Directory.GetDirectories(path))
        {
            foreach (string filePath in Directory.GetFiles(dirPath))
            {
                string filename = Path.GetFileName(filePath);
                if (filename.Equals("desktop.txt"))
                {
                    File.Delete(filePath);

                    count++;
                }
                Console.WriteLine(filePath); // print files
            }
            Console.WriteLine(dirPath); // print directories
            count += DirSearch(dirPath);
        }
    }
    catch (System.Exception excpt)
    {
        Console.WriteLine(excpt.Message);
    }

    return count;
}
于 2013-11-11T18:49:36.543 回答
2

没有 ref 变量(所以你不需要传递正确初始化的东西):

static int DirSearch(string path)
{
    try
    {
        int count = 0;
        foreach (string dirPath in Directory.GetDirectories(path))
        {
            foreach (string filePath in Directory.GetFiles(dirPath))
            {
                string filename = Path.GetFileName(filePath);
                if (filename.Equals("desktop.txt"))
                {
                    File.Delete(filePath);
                    count++;
                }
                Console.WriteLine(filePath); // print files
            }
            Console.WriteLine(dirPath); // print directories
            count += DirSearch(dirPath);
        }
        return count;
    }
    catch (System.Exception excpt)
    {
        Console.WriteLine(excpt.Message);
    }
}
于 2013-11-11T18:51:12.170 回答
0

您是否还考虑过这里介绍的非递归解决方案?https://stackoverflow.com/a/929418/2979680

于 2013-11-11T22:07:02.477 回答