简体   繁体   English

查找系统中定义的文件夹层次结构中的所有文件

[英]Find all files in system that are located inside defined hierarchy of folders

I'd like to find all files in system with name TestName.lol that lie inside TestRoot\\TestSubFoler\\Test\\ . 我想在系统中找到名为TestName.lol所有文件,它们位于TestRoot\\TestSubFoler\\Test\\

Ie if a have TestRoot\\TestSubFoler\\Test\\TestName.lol both on disk C and D , then I'd like to obtain 2 files. 即如果在磁盘CD上都有TestRoot\\TestSubFoler\\Test\\TestName.lol ,那么我想获得2个文件。

I considered different ways to do this: 我考虑过不同的方法:

  1. start with C:\\ and D:\\ and then recursively search for TestRoot . C:\\D:\\ ,然后递归搜索TestRoot Then do the same for all found occurances instead of C:\\ and D:\\ . 然后对所有发现的事件执行相同操作,而不是C:\\D:\\ And so on. 等等。

  2. recursively search all folders for TestName.lol . 递归搜索TestName.lol所有文件夹。 Then filter out those located in 'wrong' folders. 然后过滤掉位于“错误”文件夹中的文件。 (I believe that in my case this will be faster, as the estimated number of such files is not large: 1-10) (我相信在我的情况下这会更快,因为这些文件的估计数量并不大:1-10)

  3. ??? ??? I believe there can be a better way to this (more elegant or with better performance). 我相信可以有更好的方法(更优雅或更好的性能)。 Anyway, if you think my solution is OK, just confirm pls. 无论如何,如果您认为我的解决方案没问题,请确认一下。

Thanks. 谢谢。

Edit: Search file in directory using complex pattern a similar question. 编辑: 使用复杂模式在目录中搜索文件类似的问题。

Take a look at the Directory.GetFiles method. 看一下Directory.GetFiles方法。 That should do what you want. 那应该做你想要的。

Actually, this overload is probably the one you need. 实际上, 这种重载可能是你需要的。

Or, you could call Directory.EnumeratFiles , which will do the recursion for you. 或者,您可以调用Directory.EnumeratFiles ,它将为您执行递归。

Your option #2 is probably the way to go. 您的选择#2可能是要走的路。

There really isn't a particularly efficient way to do this. 确实没有一种特别有效的方法可以做到这一点。 You can speed it up somewhat by making direct calls to the Windows FindFirst and FindNext API functions, but it's really not worth the effort unless you're doing this a lot. 您可以通过直接调用Windows FindFirstFindNext API函数来加快速度,但除非您这么做,否则它确实不值得。

I personally feel it is difficult to avoid recursion . 我个人觉得很难避免递归。 Because the file system is not Indexed. 因为文件系统没有索引。 Google Desktop or Microsoft Desktop search indexed all files. Google桌面或Microsoft桌面搜索索引所有文件。 there if you query you will get the answer fairly very quick. 如果你查询,你会得到相当快的答案。

The choice we have is .net framework does recursion for you or you do it yourself. 我们的选择是.net框架为您做递归,或者您自己做。

Other option is Linq - which i guess .net framework will do the recursion . 其他选项是Linq - 我猜.net框架将进行递归。 but it will be cleaner 但它会更清洁

Linq LINQ

http://msdn.microsoft.com/en-us/library/bb882649.aspx http://msdn.microsoft.com/en-us/library/bb882649.aspx

// Take a snapshot of the file system.
        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(startFolder);

        // This method assumes that the application has discovery permissions 
        // for all folders under the specified path.
        IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories);

        string searchTerm = @"Visual Studio";

        // Search the contents of each file. 
        // A regular expression created with the RegEx class 
        // could be used instead of the Contains method. 
        // queryMatchingFiles is an IEnumerable<string>. 
        var queryMatchingFiles =
            from file in fileList
            where file.Extension == ".htm" 
            let fileText = GetFileText(file.FullName)
            where fileText.Contains(searchTerm)
            select file.FullName;

        // Execute the query.
        Console.WriteLine("The term \"{0}\" was found in:", searchTerm);
        foreach (string filename in queryMatchingFiles)
        {
            Console.WriteLine(filename);
        }

.net code .net代码

foreach (FileInfo fi in directory.GetFiles())
{
  // Console.WriteLine(@"Found file: [{0}] in directory: [{1}]", fi.Name, directory.FullName);

}
 foreach (DirectoryInfo diSourceSubDir in directory.GetDirectories())
 {
    // Console.WriteLine(@"Sub Folder {0} found.", diSourceSubDir.FullName);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM