为什么枚举文件返回相同文件超过一次? [英] Why is enumerate files returning the same file more than once?

查看:104
本文介绍了为什么枚举文件返回相同文件超过一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个code(感谢那些一直在帮助到目前为止)

I have this code (thanks to those that have been helping so far)

将搜索的目录及其所有子目录中寻找一个文件名。

It searches through a directory and all subdirectories looking for a file names.

Files.Clear(); //BindingList<FileInfo> Datasource for a datagridview

Task.Factory.StartNew( () =>
    {
       DirectoryInfo dir = new DirectoryInfo(MainFolder);

       foreach(var file in dir.EnumerateFiles("*" + textBox1.Text + "*.doc?", SearchOption.AllDirectories).Take(200))
       {
          this.BeginInvoke( new Action(() =>
             {
                Files.Add(file);
             }));
       }
     });

现在的问题是,如果我设置 textBox1.text 的东西,我知道只有1中,将其添加到文件 4倍。我想休息指着它是确定它是不是在我是如何显示出来。

The problem is if I set textBox1.text to something I know there is only 1 of, it adds it to Files 4 times. I tried break pointing it to be certain it wasn't in how I was displaying it.

我比较了4对象彼此,它们相同。当我打开搜索条件一点点,并得到5个结果,其中有些是1一些双打一些三倍。所以有5种独特的结果,但有一个总的约10-12是

I compared the 4 objects to each other, they are identical. When I open up the search criteria a little and get 5 results, some of them are 1 some are doubles some triples. so there are 5 unique results but there is a total of about 10-12.

我是什么做错了吗?

推荐答案

使用调用。

您的lambda的捕获正被突变的变量文件。您不但能够得到重复,你还丢失的文件。

Your lambda is capturing the variable file which is being mutated. You are not only getting duplicates, you are also missing files.

这篇关于为什么枚举文件返回相同文件超过一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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