C# 如何使用按钮c在列表框中显示重复项#

C# 如何使用按钮c在列表框中显示重复项#,c#,file,listbox,directory,duplicates,C#,File,Listbox,Directory,Duplicates,我在互联网上找不到这类问题的答案,所以我想我不妨问问自己。我需要能够按下一个按钮,然后将运行一个功能,搜索选定的文件目录和所有的子文件夹后,看看是否有相同的文件有两次。我将把所有必要的代码放在下面: public partial class fileForm : Form { private string _filelistlocation; private string _fileDest; private string _fileSource;

我在互联网上找不到这类问题的答案,所以我想我不妨问问自己。我需要能够按下一个按钮,然后将运行一个功能,搜索选定的文件目录和所有的子文件夹后,看看是否有相同的文件有两次。我将把所有必要的代码放在下面:

public partial class fileForm : Form
{
        private string _filelistlocation;
        private string _fileDest;
        private string _fileSource;

        public fileForm()
        {
            InitializeComponent();
        }


public void fileForm_Load(object sender, System.EventArgs e)
{
            _filelistlocation = textBox1.Text;
            _fileDest = labelDestRoot.Text;
            _fileSource = labelSourceRoot.Text;
}

private void button1_Click(object sender, System.EventArgs e)
{
            checkedListBox1.Items.Clear();
            listBox2.Items.Clear();
            ReadFromList();
}

private void GetDuplicates()
{
            DirectoryInfo directoryToCheck = new DirectoryInfo(@"C:\temp\Location Source");`

            FileInfo[] files = directoryToCheck.GetFiles("*.*", SearchOption.AllDirectories);
            var duplicates = files.GroupBy(x => x.Name)
                                    .Where(group => group.Count() > 1)
                                    .Select(group => group.Key);//display duplicates
            if (duplicates.Count() > 0)
            {
                MessageBox.Show("No Dupes");
                FileStream s2 = new FileStream(_filelistlocation, FileMode.Open, FileAccess.Read, FileShare.Read);
            }
            else
                listBox1.Items.Add(duplicates);`
        }

private void button5_Click_1(object sender, EventArgs e)
{
             GetDuplicates();
}
编辑: `私有void GetDuplicates() { DirectoryInfo directoryToCheck=新的DirectoryInfo(@“C:\temp\Location Source”)

像这样

编辑2:

private void GetDuplicates()
    {
        DirectoryInfo directoryToCheck = new DirectoryInfo(@"C:\temp\Location Source");

        FileInfo[] files = directoryToCheck.GetFiles("*.*", SearchOption.AllDirectories);
        var duplicates = files.GroupBy(x => x.Name)
                                .Where(group => group.Count() > 1)
                                .Select(group => group.Key);//display duplicates
        if (!duplicates.Any())
        {
            listBox1.Items.Add(duplicates.ToArray());

            FileStream s2 = new FileStream(_filelistlocation, FileMode.Open, FileAccess.Read, FileShare.Read);



        }
        else
            MessageBox.Show("No Dupes");
    }

如果有人能帮忙,那就太好了。谢谢!

根据评论,如果有重复项,您将显示“无重复项””消息。如果有重复项,请反转您的

if (duplicates.Count() <= 0)
{
    MessageBox.Show("No Dupes");
    FileStream s2 = new FileStream(_filelistlocation, FileMode.Open, FileAccess.Read, FileShare.Read);
}
else
{
    listBox1.Items.AddRange(duplicates);
}
if(duplicates.Count()类似以下内容:

  // Fluent: given directory name obtain duplicates with no temporarys
  var duplicates = Directory
    .EnumerateFiles(@"C:\temp\Location Source", "*.*", SearchOption.AllDirectories)
    .GroupBy(file => Path.GetFileName(file))
    .Where(chunk => chunk.Count() > 1)
    .Select(chunk => chunk.Key);

  // Straightforward: are there ANY duplicates?
  if (duplicates.Any()) { 
    // Please note "AddRange" (you have many items to add)
    listBox1.Items.AddRange(duplicates.ToArray());

    //TODO: may be it should be put into "else" (no dups)
    // Looks that you've forgot to wrap IDisposable into using...
    using (FileStream s2 = new FileStream(_filelistlocation, 
      FileMode.Open, 
      FileAccess.Read, 
      FileShare.Read)) {
      ...
    }
  }
  else {
    MessageBox.Show("No Dupes");
  } 

您在发布代码时遇到的问题是什么?当我单击按钮时,消息框会弹出“无重复项”,而我正在测试的内容却有重复项。因为您在if块上显示消息,它应该在else块上。if(duplicates.Count()>0)应该是if(!duplicates.Any())我更改了它,现在在列表框中显示了这个“System.Linq.Enumerable迭代器`2[System.Linq}…@Robert Vassitha:很抱歉输入错误:它应该是
chunk.Count()
而不是
chunk.Count
AddRange(duplicates.ToArray())
而不是
AddRange(duplicates)
;我已经编辑了答案什么见鬼!它真的起作用了?!我有点惊讶和困惑,因为我已经有了.Toarray()和.count(从以前的0开始,当我再次尝试时,它起作用了吗?我想它可能是缩进,但visual studio会告诉我,如果是这样的话。无论如何,谢谢!
  // Fluent: given directory name obtain duplicates with no temporarys
  var duplicates = Directory
    .EnumerateFiles(@"C:\temp\Location Source", "*.*", SearchOption.AllDirectories)
    .GroupBy(file => Path.GetFileName(file))
    .Where(chunk => chunk.Count() > 1)
    .Select(chunk => chunk.Key);

  // Straightforward: are there ANY duplicates?
  if (duplicates.Any()) { 
    // Please note "AddRange" (you have many items to add)
    listBox1.Items.AddRange(duplicates.ToArray());

    //TODO: may be it should be put into "else" (no dups)
    // Looks that you've forgot to wrap IDisposable into using...
    using (FileStream s2 = new FileStream(_filelistlocation, 
      FileMode.Open, 
      FileAccess.Read, 
      FileShare.Read)) {
      ...
    }
  }
  else {
    MessageBox.Show("No Dupes");
  }