Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查是否在文本文件中找到项_C#_Duplicates_Streamwriter - Fatal编程技术网

C# 检查是否在文本文件中找到项

C# 检查是否在文本文件中找到项,c#,duplicates,streamwriter,C#,Duplicates,Streamwriter,如何检查文本文件是否包含列表框中的项目。停止保存重复项。我不知道我要补充什么。这在按钮单击事件中调用。例如,如果发现重复项,我可以显示MessageBox.show(“重复错误”) stringfilepath=“test.txt”; var existingLines=newhashset(File.ReadAllLines(filePath)); var linesToWrite=新列表(); foreach(listBox2.Items中的字符串项) { 如果(现有行。添加(项目)) {

如何检查文本文件是否包含列表框中的项目。停止保存重复项。我不知道我要补充什么。这在按钮单击事件中调用。例如,如果发现重复项,我可以显示
MessageBox.show(“重复错误”)

stringfilepath=“test.txt”;
var existingLines=newhashset(File.ReadAllLines(filePath));
var linesToWrite=新列表();
foreach(listBox2.Items中的字符串项)
{
如果(现有行。添加(项目))
{
linesToWrite.Add(项目);
}
其他的
{
//这是复制品!!!
}
}
追加所有行(文件路径,linesToWrite);
字符串filePath=“test.txt”;
var existingLines=newhashset(File.ReadAllLines(filePath));
var linesToWrite=新列表();
foreach(listBox2.Items中的字符串项)
{
如果(现有行。添加(项目))
{
linesToWrite.Add(项目);
}
其他的
{
//这是复制品!!!
}
}
追加所有行(文件路径,linesToWrite);
在写入“test.txt”之前,请列举其内容:

var fileLines = File.ReadAllLines("test.txt");
List<string> fileItems = new List<string>(fileLines);
编辑:

根据建议,您可以使用
哈希集
而不是
列表
来提高性能,因为它只能包含唯一的值

另一个改进可能是在将任何内容写入文件之前检查是否存在任何重复项。在下面的示例中,我在LINQ语句中这样做了:

var fileLines = File.ReadAllLines("test.txt");
HashSet<string> fileItems = new HashSet<string>(fileLines);

using (StreamWriter writer = new StreamWriter("test.txt", true))
{
    bool duplicateFound = fileItems.Any(fi => listBox1.Items.Cast<string>().Any(i => i == fi));

    if (duplicateFound)
        MessageBox.Show("Duplicate items found.");
    else
        foreach (object item in listBox1.Items)
            writer.WriteLine(item.ToString());
}
var fileLines=File.ReadAllLines(“test.txt”);
HashSet fileItems=新的HashSet(文件行);
使用(StreamWriter=newstreamwriter(“test.txt”,true))
{
bool duplicateFound=fileItems.Any(fi=>listBox1.Items.Cast().Any(i=>i==fi));
如果(发现重复项)
Show(“找到重复项”);
其他的
foreach(listBox1.Items中的对象项)
writer.WriteLine(item.ToString());
}
编辑2:

正如@Servy所建议的,listbox可能包含重复项,这也应该考虑在内。此外,我的HashSet实现还不够理想。因此,在第三个示例中,我首先检查listbox是否包含重复项,然后检查文件中是否已有任何listbox项。HashSet的使用也更高效,因为我没有迭代它

var fileLines = File.ReadAllLines("test.txt");
HashSet<string> fileItems = new HashSet<string>(fileLines);
List<string> duplicateListboxItems = listBox1.Items.Cast<string>().GroupBy(l => l).Where(g => g.Count() > 1).Select(g => g.Key).ToList();
if (duplicateListboxItems.Count > 0)
{
    MessageBox.Show("The listbox contains duplicate entries.");
    return;
}

bool duplicateFound = false;
List<string> outputItems = new List<string>();
foreach (string item in listBox1.Items)
{
    if (fileItems.Contains(item))
    {
        MessageBox.Show(String.Format("The file has a duplicate: {0}", item));
        duplicateFound = true;
        break;
    }
    outputItems.Add(item);
}

if (duplicateFound)
    return;

using (StreamWriter writer = new StreamWriter("test.txt", true))
{
    foreach (string s in outputItems)
        writer.WriteLine(s);
}
var fileLines=File.ReadAllLines(“test.txt”);
HashSet fileItems=新的HashSet(文件行);
列出重复的ListBoxItems=listBox1.Items.Cast().GroupBy(l=>l).Where(g=>g.Count()>1).选择(g=>g.Key).ToList();
如果(duplicateListboxItems.Count>0)
{
Show(“列表框包含重复的条目”);
返回;
}
bool duplicateFound=false;
列表输出项=新列表();
foreach(listBox1.Items中的字符串项)
{
if(fileItems.Contains(item))
{
Show(String.Format(“该文件有一个副本:{0}”,item));
duplicateFound=true;
打破
}
输出项。添加(项);
}
如果(发现重复项)
返回;
使用(StreamWriter=newstreamwriter(“test.txt”,true))
{
foreach(输出项中的字符串s)
作家、作家行(s);
}
在写入“test.txt”之前,请列举其内容:

var fileLines = File.ReadAllLines("test.txt");
List<string> fileItems = new List<string>(fileLines);
编辑:

根据建议,您可以使用
哈希集
而不是
列表
来提高性能,因为它只能包含唯一的值

另一个改进可能是在将任何内容写入文件之前检查是否存在任何重复项。在下面的示例中,我在LINQ语句中这样做了:

var fileLines = File.ReadAllLines("test.txt");
HashSet<string> fileItems = new HashSet<string>(fileLines);

using (StreamWriter writer = new StreamWriter("test.txt", true))
{
    bool duplicateFound = fileItems.Any(fi => listBox1.Items.Cast<string>().Any(i => i == fi));

    if (duplicateFound)
        MessageBox.Show("Duplicate items found.");
    else
        foreach (object item in listBox1.Items)
            writer.WriteLine(item.ToString());
}
var fileLines=File.ReadAllLines(“test.txt”);
HashSet fileItems=新的HashSet(文件行);
使用(StreamWriter=newstreamwriter(“test.txt”,true))
{
bool duplicateFound=fileItems.Any(fi=>listBox1.Items.Cast().Any(i=>i==fi));
如果(发现重复项)
Show(“找到重复项”);
其他的
foreach(listBox1.Items中的对象项)
writer.WriteLine(item.ToString());
}
编辑2:

正如@Servy所建议的,listbox可能包含重复项,这也应该考虑在内。此外,我的HashSet实现还不够理想。因此,在第三个示例中,我首先检查listbox是否包含重复项,然后检查文件中是否已有任何listbox项。HashSet的使用也更高效,因为我没有迭代它

var fileLines = File.ReadAllLines("test.txt");
HashSet<string> fileItems = new HashSet<string>(fileLines);
List<string> duplicateListboxItems = listBox1.Items.Cast<string>().GroupBy(l => l).Where(g => g.Count() > 1).Select(g => g.Key).ToList();
if (duplicateListboxItems.Count > 0)
{
    MessageBox.Show("The listbox contains duplicate entries.");
    return;
}

bool duplicateFound = false;
List<string> outputItems = new List<string>();
foreach (string item in listBox1.Items)
{
    if (fileItems.Contains(item))
    {
        MessageBox.Show(String.Format("The file has a duplicate: {0}", item));
        duplicateFound = true;
        break;
    }
    outputItems.Add(item);
}

if (duplicateFound)
    return;

using (StreamWriter writer = new StreamWriter("test.txt", true))
{
    foreach (string s in outputItems)
        writer.WriteLine(s);
}
var fileLines=File.ReadAllLines(“test.txt”);
HashSet fileItems=新的HashSet(文件行);
列出重复的ListBoxItems=listBox1.Items.Cast().GroupBy(l=>l).Where(g=>g.Count()>1).选择(g=>g.Key).ToList();
如果(duplicateListboxItems.Count>0)
{
Show(“列表框包含重复的条目”);
返回;
}
bool duplicateFound=false;
列表输出项=新列表();
foreach(listBox1.Items中的字符串项)
{
if(fileItems.Contains(item))
{
Show(String.Format(“该文件有一个副本:{0}”,item));
duplicateFound=true;
打破
}
输出项。添加(项);
}
如果(发现重复项)
返回;
使用(StreamWriter=newstreamwriter(“test.txt”,true))
{
foreach(输出项中的字符串s)
作家、作家行(s);
}

您可以将
fileItems
更改为
HashSet
,这将大大加快运行速度。你不需要改变其他任何事情。您还需要将列表框中的项添加到
foreach
中的
fileItems
。事实上,如果更改为HashSet,它会变得更容易,因为您不需要检查包含项,如果该项已存在且不存在,则add将返回true/falseadded@Alex是的,修改了我的答案,因为它太干净了。你仍然没有在列表框中捕捉到任何重复的项目。您对hashSet的使用也非常糟糕。当您迭代一个哈希集,然后对每个项迭代整个列表框时,您完全无法达到使用该哈希集的目的。您应该检查每个列表框项,看看hashset是否包含