搜索文件 [英] Searching for files

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

问题描述

大家好,

我正在尝试在目录中搜索文件.我想为该方法提供一个目录以供搜索,并提供所需的文件名,它必须在目录中查找,并为我提供具有该名称的所有文件.我目前正在使用:

Hi there everyone,

I am trying to search for files in a directory. I want to give the method a directory to search in and the name of the files I want and it must look in the directory and get me all the files with that name. I am currently using:

SearchedFiles = Directory.GetFiles(str_Directory, str_Name, SearchOption.AllDirectories);



其中,搜索​​到的文件"是一个字符串数组,"Directory.GetFiles"方法返回一个包含所有找到的文件名的数组.

问题是,如果存在受读取保护的目录,则该方法将中止并引发异常"UnauthorizedAccessException".

有没有一种方法可以搜索文件而只是忽略所有无法读取的目录,而不是方法异常中止并引发异常.如果有人知道更好的搜索方法,也可能会有帮助.

谢谢
Grimes



where Searched files is an array of strings and the Directory.GetFiles method returns an array with all the file names found.

The problem is that if there are directories that are read protected, the method aborts and throws an exception "UnauthorizedAccessException".

Is there a way to search for files and just ignore all the directories that can''t be read, instead of the method aborting and throwing the exception. It could also help if someone knows of an better search Method.

Thanks
Grimes

推荐答案

您可以使用部分递归方法. (部分是因为在理想情况下,递归部分是由框架使用"AllDirectories"处理的)

而不是一次调用就获取所有文件,而是对其进行构建.

因此方法可能类似于

You can use a partially recursive method. (partially because in an ideal case the recursive part is handled by the framework using "AllDirectories")

Rather than getting all the files on one call build them up.

so the method might be something like

 void RecursiveFileFinder(string path, List<string> collection)
{
   DirectoryInfo dInfo = null;
   try
   {
        dInfo = new DirectoryInfo(path);
   }
   catch(UnauthorizedAccessException)
   {
       //This is the directory that is causing the failure
       //No need to process this directory
       return collection;
   }  
try //Process all sub dirs if possible
{
  string[] foundFiles = dInfo.GetFiles(str_Name,SearchOption.AllDirectories);
    collection.AddRange(foundFiles);
}
catch(UnauthorizedAccessException)
{
      //Call for each dir since we don''t know which failed
      var subDirs = dInfo.GetDirectories();
      foreach(var subDir in subDirs)
      {
          RecursiveFileFinder(string path, List<string> collection);
      }
}
}
</string></string>



我没有测试过,但是之前我必须做类似的事情,它才能解决问题.



I havn''t tested it but I had to do something similar to this before and it resolved the issue.


如果您的搜索方法是常规"递归类型,则您可以捕获有问题的异常并忽略它们.

像这样的东西:
If your search method is of the ''usual'' recursive type, you can catch the problematic exceptions and ignore them.

Something like:
try
{
    files = Directory.GetFiles(directory, search);
}
catch (UnauthorizedAccessException)
{
    return;
}
catch (DirectoryNotFoundException)
{
    return;
}


您可能应该为其他异常添加第三个捕获,将其显示(MessageBox,Console ...任何内容),以便随后可以决定是否为其添加特定的异常处理程序.


You should probably add a third catch for other exceptions, displaying them (MessageBox, Console...whatever) so that you can then decide whether to add a specific exception handler for them.


谢谢为大家提供帮助.我以为可以使用一种简单的方法来解决此问题,但是看来我不得不编写自己的递归搜索功能.

我的方法基于Collin的代码,因此请多谢.这是我写的代码:

Thanks for everyones help. I thought that there would be an easy method to use to fix the problem, but it seems that I had to write my own recursive search function.

My method was based on Collin''s code, so thanx for the help. Here is the code that I wrote:

public static ArrayList GetFiles(string SearchPath, string FileName)
        {
            //Temporarily stores the files found
            ArrayList TempFileNames = new ArrayList();
            try
            {
                //get all the sub directories
                DirectoryInfo DirInfo = new DirectoryInfo(SearchPath);
                DirectoryInfo[] SubDirs = DirInfo.GetDirectories();
                for (int i = 0; i < SubDirs.Length; i++)
                {
                    //check inside each sub-dir (Recursion)                   
                    TempFileNames.AddRange(GetFiles(SubDirs[i].FullName, FileName));
                }
                //after searching sub-dirs look in the current dir
                TempFileNames.AddRange(Directory.GetFiles(SearchPath, FileName, SearchOption.TopDirectoryOnly));
            }
            //Handle exceptions for unreadable files
            catch (UnauthorizedAccessException ex)
            { 
                return TempFileNames;
            }
            catch (DirectoryNotFoundException ex)
            {
                return TempFileNames;
            }
            return TempFileNames;
        }



我已经对其进行了测试,并且可以正常工作并且忽略了不可读的目录.希望这对下一个家伙有所帮助;)



I have tested it and it works and ignores the unreadable directories as it should. Hopefully this helps the next guy ;)


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

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