gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:18:55 25 4
gpt4 key购买 nike

我在互联网上的任何地方都找不到此类问题的答案,所以我想我不妨问问自己。我需要能够按下一个按钮,然后该按钮将运行一个功能,该功能会搜索所选文件目录以及之后的所有子文件夹,以查看同一个文件是否存在两次。我将把所有必要的代码放在下面:

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();
}

编辑:`私有(private)无效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);

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



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

像这样吗?

编辑 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");
}

如果有人能提供帮助那就太好了。谢谢!

最佳答案

根据评论,当有重复时,您会显示“No dupes”消息。 反转你的if语句

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

关于c# - 如何使用按钮 c# 在列表框中显示重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36126679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com