在C#中搜索子目录 [英] Searching Subdirectories in C#

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

问题描述

我的文件名列表,我想搜索的目录及其所有子目录。这些目录包含每个约20万文件。我的code发现了该文件,但它需要每个文件约20分钟。有人建议可以更好的方法?

Ç段

$ C $

 的String [] file_names = File.ReadAllLines(@C:\ file.txt的);
的foreach(在file_names串FILE_NAME)
{
    字符串[]文件= Directory.GetFiles(@我:\ PAX \,FILE_NAME +名.txt,
                                        SearchOption.AllDirectories);
    的foreach(在文件中字符串的文件)
    {
        System.IO.File.Copy(文件,
                            @C:\+
                            textBox1.Text + @\ n \ o \+
                            FILE_NAME +
                            。TXT
                            );
    }

}
 

解决方案

如果你要搜索多个文件在​​同一目录结构,你应该找到的所有的的在目录结构中,一旦文件,然后通过他们搜索在存储器中。有没有必要去到文件系统中一遍又一遍。

编辑:有这样一来,使用LINQ的一个优雅的方式 - 和不太优雅的方式,没有。这里的LINQ方式:

 使用系统;
使用System.IO;
使用System.Linq的;

类测试
{
    静态无效的主要()
    {
        //这创建从文件名的查找,以该组
        包含该文件//目录
        VAR TEXTFILES =
            Directory.GetFiles(我:\\ PAX,* .TXT,SearchOption.AllDirectories)
                     .ToLookup(文件=> Path.GetFileName(文件),
                               文件=> Path.GetDirectoryName(文件));

        字符串[]文件名= File.ReadAllLines(@C:\ file.txt的);
        //删除引号为您的真实code :)
        字符串targetDirectory =C:\\+textBox1.Text+ @\\ñ\\Ø\\;

        的foreach(在文件名字符串文件名)
        {
            字符串TMP =文件名+名.txt;
            的foreach(在TEXTFILES字符串目录[TMP])
            {
                字符串源= Path.Combine(目录,TMP);
                目标字符串= Path.Combine(targetDirectory,TMP);
                File.Copy(源,目标);
            }
        }
    }
}
 

让我知道如果你需要非LINQ的方式。有一件事要检查之前,我这样做,虽然 - 这可能在彼此的顶部复制多个文件。是的真正的,你想做什么? (想象一下, a.txt中存在于多个地方,而一是在文件中。)

I have a list of file names, and I want to search a directory and all its subdirectories. These directories contain about 200,000 files each. My code finds the the file but it takes about 20 minutes per file. Can someone suggest a better method?

Code Snippet

String[] file_names = File.ReadAllLines(@"C:\file.txt");
foreach(string file_name in file_names) 
{
    string[] files = Directory.GetFiles(@"I:\pax\", file_name + ".txt",
                                        SearchOption.AllDirectories);
    foreach(string file in files)
    {
        System.IO.File.Copy(file, 
                            @"C:\" + 
                            textBox1.Text + @"\N\O\" + 
                            file_name + 
                            ".txt"
                            );
    }

}

解决方案

If you're searching for multiple files in the same directory structure, you should find all the files in that directory structure once, and then search through them in memory. There's no need to go to the file system again and again.

EDIT: There's an elegant way of doing this, with LINQ - and the less elegant way, without. Here's the LINQ way:

using System;
using System.IO;
using System.Linq;

class Test
{
    static void Main()
    {
        // This creates a lookup from filename to the set of 
        // directories containing that file
        var textFiles = 
            Directory.GetFiles("I:\\pax", "*.txt", SearchOption.AllDirectories)
                     .ToLookup(file => Path.GetFileName(file),
                               file => Path.GetDirectoryName(file));

        string[] fileNames = File.ReadAllLines(@"c:\file.txt");
        // Remove the quotes for your real code :)
        string targetDirectory = "C:\\" + "textBox1.Text" + @"\\N\\O\\";

        foreach (string fileName in fileNames)
        {
            string tmp = fileName + ".txt";
            foreach (string directory in textFiles[tmp])
            {
                string source = Path.Combine(directory, tmp);
                string target = Path.Combine(targetDirectory, tmp);
                File.Copy(source, target);                                       
            }
        }
    }
}

Let me know if you need the non-LINQ way. One thing to check before I do so though - this could copy multiple files over the top of each other. Is that really what you want to do? (Imagine that a.txt exists in multiple places, and "a" is in the file.)

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

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