KP123
KP123

Reputation: 71

Add strings to the end of certain lines in a text document and write to an empty text doc in C#

I am reading a text file and picking out certain keywords and adding strings to them and then writing it to another empty text document. My problem is that since I am doing this to multiple strings, it writes the text file multiple times over and I end up with 5 additional lines for every line in the original text document. Anyone know another method that I could use to get this done?

var fileContents = System.IO.File.ReadAllLines(textBox5.Text);
        var outFileContents = new List<string>();
        foreach (var line in fileContents)
        {

            if (line.Contains("Start **** Connect Process"))  //Text to find 
                outFileContents.Add(line + "," + Environment.NewLine + "*****" + Environment.NewLine); //Add your text
            else
                outFileContents.Add(line + ","); //Keep column

            if (line.Contains("Start $$$$$$ Connect Process"))
                outFileContents.Add(line + "," + Environment.NewLine + "$$$$$$" + Environment.NewLine);
            else
                outFileContents.Add(line + ",");
            if (line.Contains("Fail to send &&&&&&&&"))
                outFileContents.Add(line + "," + Environment.NewLine + "&&&&&&" + Environment.NewLine);
            else
                outFileContents.Add(line + ",");

            if (line.Contains("Start @@@@@ Process"))
                outFileContents.Add(line + "," + Environment.NewLine + "@@@@@@" + Environment.NewLine);
            else
                outFileContents.Add(line + ",");
            if (line.Contains("ConnectionStatus: ######"))
                outFileContents.Add(line + "," + Environment.NewLine + "######" + Environment.NewLine);
            else
                outFileContents.Add(line + ",");

            System.IO.File.WriteAllLines(textBox6.Text, outFileContents);
        }



        Process.Start(textBox6.Text);

    }

Upvotes: 0

Views: 100

Answers (3)

Jim Mischel
Jim Mischel

Reputation: 134015

The problem isn't because you're calling WriteAllLines in a loop. WriteAllLines overwrites the file. Just see the documentation.

The problem is that you're adding every line to the output list five times.

What you have is essentially:

for every line
    if ()
        add modified line
    else
        add unmodified line

    if ()
        add modified line
    else
        add unmodified line

Five of those conditionals means five copies of the line being added to the output list.

You need to build your string in a temporary buffer and add it to the list just once. Something like:

for(...)
{
    StringBuilder sb = new StringBuilder(line);

    if (line.Contains("Start **** Connect Process"))
        sb.Append("," + Environment.NewLine + "*****" + Environment.NewLine);
    else
        sb.Append(',');

    // do that for each of your conditionals.

    // and finally, add the line to the output buffer:
    outFileContents.Add(sb.ToString());
}

Now, you should remove the WriteAllLines out of the loop so that you're not rewriting the file every time. So your code becomes:

for (....)
{
    // do stuff
}
File.WriteAllLines(...)

Upvotes: 3

Ian
Ian

Reputation: 34489

The reason is because you're writing the text over and over (it's in a loop). Instead you need to write to the file just once at the end of your operation by moving the WriteAllLines method call.

Additionally you might want to think about using the StringBuilder class for generating your contents.

var fileContents = System.IO.File.ReadAllLines(textBox5.Text);
    var outFileContents = new List<string>();
    foreach (var line in fileContents)
    {

        if (line.Contains("Start **** Connect Process"))  //Text to find 
            outFileContents.Add(line + "," + Environment.NewLine + "*****" + Environment.NewLine); //Add your text
        else
            outFileContents.Add(line + ","); //Keep column

        if (line.Contains("Start $$$$$$ Connect Process"))
            outFileContents.Add(line + "," + Environment.NewLine + "$$$$$$" + Environment.NewLine);
        else
            outFileContents.Add(line + ",");
        if (line.Contains("Fail to send &&&&&&&&"))
            outFileContents.Add(line + "," + Environment.NewLine + "&&&&&&" + Environment.NewLine);
        else
            outFileContents.Add(line + ",");

        if (line.Contains("Start @@@@@ Process"))
            outFileContents.Add(line + "," + Environment.NewLine + "@@@@@@" + Environment.NewLine);
        else
            outFileContents.Add(line + ",");
        if (line.Contains("ConnectionStatus: ######"))
            outFileContents.Add(line + "," + Environment.NewLine + "######" + Environment.NewLine);
        else
            outFileContents.Add(line + ",");
    }

    System.IO.File.WriteAllLines(textBox6.Text, outFileContents);

    Process.Start(textBox6.Text);

Upvotes: 0

BRAHIM Kamel
BRAHIM Kamel

Reputation: 13765

get this System.IO.File.WriteAllLines(textBox6.Text, outFileContents); out of foreach

 var fileContents = System.IO.File.ReadAllLines(textBox5.Text);
            var outFileContents = new List<string>();
            foreach (var line in fileContents)
            {




            }
     System.IO.File.WriteAllLines(textBox6.Text, outFileContents);


            Process.Start(textBox6.Text);

        }

Upvotes: 0

Related Questions