搜索中的目录和子目录的文件的最快的方法 [英] Fastest Way of Searching a file within Directories and SubDirectories

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

问题描述

我要搜索的目录和子目录中的文件的最快方式。文件的数量超过140万。文件大小范围5至50 KB。每个文件夹包含5到10000个文件。

目前我使用的时刻:

 的foreach(在Directory.GetFiles字符串文件(路径,*。*,SearchOption.AllDirectories))
{
    如果(file.Contains(searchkeyword))
    {
        字符串AnchorText = Path.GetFileNameWithoutExtension(文件);
    }
}


解决方案

最佳的解决方案

我会索引添加到该目录并使用<一个href=\"http://stackoverflow.com/questions/4131420/querying-windows-file-indexing-service-using-c-sharp\">Windows搜索查找文件,在它的。这里的优势不仅在于它是不是你需要维护,但也是Windows索引服务能够索引文件(.DOC,.PDF,.TXT等)的内容。

土生土长的解决方案

一个数据库里有一个土生土长缓存(* SQL中,Redis的,蒙戈,Lucene的,不管你preFER)。


  1. 订阅一个 FileSystemWatcher的 的目录,当你启动应用程序。

  2. 然后在目录中的一次的(按你原来的code样品),以确保没有文件被添加/删除/而你的应用程序没有运行改变。
  3. 在数据库中存储从这两任何数据在一个适当的索引表。

当你需要搜索的文件只是查询数据库。

I need the fastest way to search for files within directories and subdirectories. The number of files is more than 1.4 Million. File sizes range between 5 and 50 KB. Each folder contains 5 to 10000 files.

At the moment I am using:

foreach (string file in Directory.GetFiles(path, "*.*", SearchOption.AllDirectories))
{
    if (file.Contains(searchkeyword))
    {
        string AnchorText = Path.GetFileNameWithoutExtension(file);
    }
}

解决方案

Best Solution

I would add an index to that directory and use Windows Search to find files in it. The advantages here are not only that it isn't code that you need to maintain but also that the Windows Indexing Service is able to index the contents of files (.doc, .pdf, .txt, etc.).

Home-Grown Solution

A home-grown cache inside of a database (*Sql, Redis, Mongo, Lucene, whatever you prefer).

  1. Subscribe a FileSystemWatcher to the directory when your application starts.
  2. Then perform an enumeration over the directory once (as per your original code sample) to ensure that no files were added/removed/changed while your application was not running.
  3. Store any data from these two in an appropriately indexed table in a database.

When you need to search for a file simply query the database.

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

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