使用C#检查TXT文件中是否存在重复项 [英] Check if there are duplicates in TXT files using C#

查看:380
本文介绍了使用C#检查TXT文件中是否存在重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿大家,



我正在尝试制作一个C#代码,以确定文本框字段的输入是否已经存在于TXT文件中。 />
目前我有以下代码来存储文本框中的输入。



我想要的是一个检查,它通过TextSA.txt文件,只检查ChildOrderInput.Text,如果它已经存在于该TXT文件中。在此检查中应忽略Date和OrderInputText。



 File.AppendAllText(   textSA.txt,DateTime.Now.ToString(  dd-MM-yyyy)+ OrderInputText +   + ChildOrderInput .Text +   \ n + Environment.NewLine); } 



你们对如何做到这一点有什么想法吗?



此外,如果检查发现重复,它应该向用户提供一个错误,即ChildorderInput.Text已经在文件中,然后将DateTime.Now发送回用户。



希望有人可以帮助我!



谢谢!



grtz Robert



我的尝试:



 File.AppendAllText(textSA.txt,DateTime.Now.ToString(dd-MM-yyyy)+ OrderInputText ++ ChildOrderInput。文字+\ n+ Environment.NewLine); } 

解决方案

尝试:

  string  strTheFile = File.ReadAllText( @  D:\ Test Data \ textSA。 TXT); 
if (strTheFile.Contains(ChildOrderInput.Text))
{
Console.WriteLine( 找到了!);
}


你可以这样做:



  1. 逐行阅读文件。
  2. 对于每个文件:



    1. 按<$ c拆分为两部分$ c>并采取第二部分。这为您提供了以前保存的ChildOrderInput。 注意:这不起作用,OrderInputText可能包含空格;因为,如果可能,则空格不是OrderInputText和ChildOrderInput.Text的明确分隔符。因此,如果OrderInputText可以包含空格,请使用除之外的其他分隔符。
    2. 如果保存的ChildOrderInput等于当前值,则打破循环并显示错误。



(作为旁注,我没有看到的意思\ n + Environment.NewLine - 如果你想要两个换行符,最好使用 Environment.NewLine + Environment.NewLine

  bool  displayError =  false ; 
使用(StreamReader sr = new StreamReader( textSA.txt))
{
while ( sr.Peek()> -1)
{
string line = sr.ReadLine();
if (!string.IsNullOrWhiteSpace(line))
{
string parts = line.Split( new char [] {' '}, 2 );
if (parts.Length < 2 ){ / * 该行的格式不正确;如果可能发生,请正确处理* / }

string previousChildOrderInput = parts [ 1 ];
if (previousChildOrderInput.Equals(ChildOrderInput.Text))
{
displayError = ;
break ;
}
}
}
}
if (displayError)
{
// 显示错误

// 在此处显示而不是在循环内部显示该文件,确保在显示错误消息之前文件已经关闭。
}


Hey everybody,

I'm trying to make a C# code that finds out if the input from the textbox field is already in a TXT file.
Currently I have the code below to store the Input from the textbox.

What I want is a check that go's through the TextSA.txt file and only checks for the ChildOrderInput.Text and if it already exists in that TXT file. The Date and OrderInputText should be ignored in this check.

File.AppendAllText("textSA.txt", DateTime.Now.ToString("dd-MM-yyyy") + OrderInputText + " " + ChildOrderInput.Text + "\n" + Environment.NewLine); }


Do you guys have any ideas on how to do this?

Also if the check finds a duplicate it should give an error to the user that the ChildorderInput.Text is already in the file and then send the DateTime.Now back to the user.

Hope anyone can help me!

Thanks!

grtz Robert

What I have tried:

File.AppendAllText("textSA.txt", DateTime.Now.ToString("dd-MM-yyyy") + OrderInputText + " " + ChildOrderInput.Text + "\n" + Environment.NewLine); }

解决方案

Try:

string strTheFile = File.ReadAllText(@"D:\Test Data\textSA.txt");
if (strTheFile.Contains(ChildOrderInput.Text))
    {
    Console.WriteLine("Found!");
    }


You can do this:


  1. Read the file line-by-line.
  2. For each file:


    1. Split into two parts by " " and take the second part. That gives you the previously saved ChildOrderInput. Note: this will not work of OrderInputText can possibly contain spaces; because, if that is possible, a space is not a clear separator for OrderInputText and ChildOrderInput.Text. So if OrderInputText can contain spaces, use another separator than " ".
    2. If that saved ChildOrderInput equals the current one, break the loop and show the error.


(As a side note, I don't see the point of "\n" + Environment.NewLine - if you want two newlines, you better use Environment.NewLine + Environment.NewLine)

bool displayError = false;
using (StreamReader sr = new StreamReader("textSA.txt"))
{
    while (sr.Peek() > -1)
    {
        string line = sr.ReadLine();
        if (!string.IsNullOrWhiteSpace(line))
        {
            string parts = line.Split(new char[] { ' ' }, 2);
            if (parts.Length < 2) { /* the line isn't in the correct format; handle it correctly if it can happen */ }

            string previousChildOrderInput = parts[1];
            if (previousChildOrderInput.Equals(ChildOrderInput.Text))
            {
                displayError = true;
                break;
            }
        }
    }
}
if (displayError)
{
    // display error

    // Displaying it here rather than inside the loop makes sure that the file is already closed before the error message is displayed.
}


这篇关于使用C#检查TXT文件中是否存在重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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