为什么枚举文件返回相同文件超过一次?文件

2023-09-07 09:09:43 作者:就当风没吹过我没来过

我有这个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.