Counter inside a recursive function

て烟熏妆下的殇ゞ 提交于 2019-12-10 11:40:50

问题


I need to count the number of files deleted in this recursive function. Since it's recursive I cannot use if statements, and C# does not support global variables. Any alternatives?

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);
    }
}

回答1:


One way is to pass in something for it to count into. I'd do this using ref, for example:

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);
    }
}

Then call it:

int count = 0;

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

Then you can use count as normal as you had commented out in your code.




回答2:


Try a recursive count as follows. So DirSearch returns the count of deleted files.

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;
}



回答3:


Without a ref variable (so you don't need to pass something that is correctly initialized):

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);
    }
}



回答4:


Did You also consider a non-recursive solution like presented here? https://stackoverflow.com/a/929418/2979680



来源:https://stackoverflow.com/questions/19913471/counter-inside-a-recursive-function

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