-1

Here is my data result code

it gets duplicate line results

I wanna remove duplicate result

Please help me

private void DataResult(string result, string acc, string file)
{
    lock (this)
    {
        if (result == "good")
        {
            MetroTextBox metroTextBox = this.textBox1;
            metroTextBox.Text = metroTextBox.Text + acc + Environment.NewLine;
            file = Path.Combine(this.papka, "good.txt");
            if (!Directory.Exists(this.papka))
            {
                Directory.CreateDirectory(this.papka);
            }
            File.AppendAllText(file, acc + "\r\n");
            Listing.good++;
        }
        if (result == "error")
        {
            Listing.error++;
        }
    }
}
anonsec
  • 1
  • 1

3 Answers3

1

Add this to using directives at top of page:

using System.Linq;

Then simply use like so:

metroTextBox.Text = string.Join(Environment.NewLine, metroTextBox.Lines.Distinct());

In your example:

private void DataResult(string result, string acc, string file)
{
    lock (this)
    {
        if (result == "good")
        {
            MetroTextBox metroTextBox = this.textBox1;
            metroTextBox.Text = string.Join(metroTextBox.Lines.Distinct(), acc, Environment.NewLine);
            file = Path.Combine(this.papka, "good.txt");
            if (!Directory.Exists(this.papka))
            {
                Directory.CreateDirectory(this.papka);
            }
            File.AppendAllText(file, acc + "\r\n");
            Listing.good++;
        }
        if (result == "error")
        {
            Listing.error++;
        }
    }
}
Julian
  • 886
  • 10
  • 21
1

Assuming this method is the only way lines can get added to the text box, maybe you should check if the text box contains acc before you add it...

if(!metroTextBox.Text.Contains(acc))
  metroTextBox.Text = metroTextBox.Text + acc + Environment.NewLine;

Side note; if you rename your text box on the form, you won't need to establish variables to it with other names. Click the text box on the form, and in the properties grid where it says (Name) textbox1, change that to metroTextBox

Side note 2; this code appends the contents of the text box to a file every time it adds a line to the text box. This could also be a source of duplication if the file name doesn't change because after adding 3 lines your file will look like:

line1
line1
line2
line1
line2
line3

I don't recommend you write a file as often as you add a line to a text box; one operation is trivial, the other is really slow and involved. Separate these things into different methods and call write file less often

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
0

You can try collecting unique results, with a help of HashSet<string>, e.g.

private HashSet<string> m_Results = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

then

private void DataResult(string result, string acc, string file)
{
    lock (this)
    {
        if (result == "good")
        {
            if (m_Results.Add(result)) {
                 // result is unique (not in m_Results)
                 MetroTextBox metroTextBox = this.textBox1;
                 metroTextBox.Text = metroTextBox.Text + acc + Environment.NewLine;

                 file = Path.Combine(this.papka, "good.txt");

                 //DONE: no need to check for directoty exitance
                 Directory.CreateDirectory(this.papka);

                 File.AppendAllText(file, acc + "\r\n");
            }
            else {
                 // result has been appeared before 
            }

            Listing.good++;
        }

        if (result == "error")
        {
            Listing.error++;
        }
    }
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215