C#中找到一个文件夹时,路径未知,然后列出文件和回报 [英] c# find a FOLDER when path is unknown then list files and return

查看:120
本文介绍了C#中找到一个文件夹时,路径未知,然后列出文件和回报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习C#和需要找到一个文件夹时,完整的路径是未知的。一个例子是,你知道的专辑名称,但不是在音乐文件夹的艺术家。找到一个专辑名称是不是这个代码,但对于这个问题,最好的例子最终用途。我与递归这样做,限制搜索的深度。一切工作良好除非我找到该文件夹​​并列出我希望它停下并返回文件,但它并没有,它只是不断递归去即使我有文件夹。我也努力与异常处理的怎么样跳过一个文件夹,如果权限是无效的。



 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
:使用System.IO;

命名空间listFoldersTest
{
类节目
{
静态无效的主要(字串[] args)
{
控制台。 SetWindowSize(100,50);
DirectoryInfo的DIR =新的DirectoryInfo(@C:\Users\username\Music);
getDirsFiles(DIR,0,2);
Console.ReadKey();
Console.WriteLine(完成);
}
公共静态无效getDirsFiles(DirectoryInfo的D,INT currentDepth,诠释MAXDEPTH)
{
字符串folderToFindName =(ALBUMNAME);
布尔foundIt = FALSE;

如果(currentDepth< MAXDEPTH)(*。*)
{
DirectoryInfo的[] =迪尔斯d.GetDirectories;
的foreach(DirectoryInfo的DIR在显示目录)
{
字符串将pathName =(dir.FullName);
Console.WriteLine(\r {0},dir.Name);
如果(currentDepth ==(MAXDEPTH - 1))
{
如果(pathName.IndexOf(folderToFindName)!= -1)
{
foundIt = TRUE;
的FileInfo [] =文件dir.GetFiles(*。*);
的foreach(在文件的FileInfo文件)
{
Console.WriteLine(-------------------->> { 0},file.Name);
} //结束的foreach文件

} //结束时,如果如果获得文件当前深度

将pathName

} //结束如果(foundIt ==真)
{
的回报;
}
getDirsFiles(DIR,currentDepth + 1,MAXDEPTH);

} //如果最终的foreach目录

} //结束时,如果当前目录深度

} //结束getDirsFiles功能
}
}


解决方案

 使用系统; 
:使用System.IO;

命名空间listFoldersTest
{
类节目
{
私人静态布尔foundIt;

静态无效的主要(字串[] args)
{
Console.SetWindowSize(100,50);

{
DirectoryInfo的DIR =新的DirectoryInfo(参数[0]);
getDirsFiles(DIR,0,2);
}

{
}
Console.ReadKey();
Console.WriteLine(完成);
}

公共静态无效getDirsFiles(DirectoryInfo的D,INT currentDepth,诠释MAXDEPTH)
{
如果(D == NULL || foundIt)回报;
字符串folderToFindName =(ALBUMNAME);
如果(currentDepth< MAXDEPTH)
{
DirectoryInfo的[] =迪尔斯d.GetDirectories(*。*);
的foreach(DirectoryInfo的DIR在显示目录)
{
字符串将pathName =(dir.FullName);
Console.WriteLine(\r {0},dir.Name);
如果(currentDepth ==(MAXDEPTH - 1))
{
如果(pathName.IndexOf(folderToFindName)!= -1)
{
foundIt = TRUE;
的FileInfo [] =文件dir.GetFiles(*。*);
的foreach(在文件的FileInfo文件)
{
Console.WriteLine(-------------------->> { 0},file.Name);
} //结束的foreach文件
的回报;
} //结束时,如果将pathName
} //如果最终的获取文件当前深度
getDirsFiles(DIR,currentDepth + 1,MAXDEPTH);
} //如果最终的foreach目录
} //结束时,如果当前目录深度
} //结束getDirsFiles功能
}
}


I am learning c# and need to find a folder when the complete path is unknown. An example would be you know the the album name but not the artist in the music folder. Finding an album name is NOT the final usage for this code but the best example for this question. I am doing this with recursion and limiting the depth of the search. Everything works good except when I find the folder and list the files I want it to stop and return but it does not, it just keeps the recursion going even after I have the the folder. I have also struggled with exception handling like how to skip a folder if permissions are not valid.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

     namespace listFoldersTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.SetWindowSize(100, 50);
                DirectoryInfo dir = new DirectoryInfo(@"C:\Users\username\Music");
                getDirsFiles(dir, 0, 2);
                Console.ReadKey();
                Console.WriteLine("done");
            }
            public static void getDirsFiles(DirectoryInfo d, int currentDepth, int maxDepth)
            {
                String folderToFindName = ("albumName");
                bool foundIt = false;

                if (currentDepth < maxDepth)
                {
                    DirectoryInfo[] dirs = d.GetDirectories("*.*");
                    foreach (DirectoryInfo dir in dirs)
                    {
                        String pathName = (dir.FullName);
                        Console.WriteLine("\r{0} ", dir.Name);
                        if (currentDepth == (maxDepth - 1))
                        {                                                                                                                                         
                            if (pathName.IndexOf(folderToFindName) != -1)
                            {
                                foundIt = true;
                                FileInfo[] files = dir.GetFiles("*.*");
                                foreach (FileInfo file in files)
                                {
                                    Console.WriteLine("-------------------->> {0} ", file.Name);
                                } //end foreach files

                            } // end if pathName

                        } // end if of get files current depth

                        if (foundIt == true)
                        {
                            return;
                        }
                        getDirsFiles(dir, currentDepth + 1, maxDepth);

                    } //end if foreach directories

                } //end if directories current depth

            } // end getDirsFiles function
        }
    }

解决方案

using System;
using System.IO;

namespace listFoldersTest
{
  class Program
  {
    private static bool foundIt;

    static void Main(string[] args)
    {
        Console.SetWindowSize(100, 50);
        try
        {
            DirectoryInfo dir = new DirectoryInfo(args[0]);
            getDirsFiles(dir, 0, 2);
        }
        catch
        {
        }
        Console.ReadKey();
        Console.WriteLine("done");
    }

    public static void getDirsFiles(DirectoryInfo d, int currentDepth, int maxDepth)
    {
        if(d == null || foundIt) return;
        String folderToFindName = ("albumName");
        if (currentDepth < maxDepth)
        {
            DirectoryInfo[] dirs = d.GetDirectories("*.*");
            foreach (DirectoryInfo dir in dirs)
            {
                String pathName = (dir.FullName);
                Console.WriteLine("\r{0} ", dir.Name);
                if (currentDepth == (maxDepth - 1))
                {
                    if (pathName.IndexOf(folderToFindName) != -1)
                    {
                        foundIt = true;
                        FileInfo[] files = dir.GetFiles("*.*");
                        foreach (FileInfo file in files)
                        {
                            Console.WriteLine("-------------------->> {0} ", file.Name);
                        } //end foreach files
                        return;
                    } // end if pathName
                } // end if of get files current depth
                getDirsFiles(dir, currentDepth + 1, maxDepth);
            } //end if foreach directories
        } //end if directories current depth
    } // end getDirsFiles function
  }
}

这篇关于C#中找到一个文件夹时,路径未知,然后列出文件和回报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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