Click here to Skip to main content
15,914,162 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ClassLibrary3
{
    public class Class1
    {
       string Line;

        char [] sep = new char[] {" " };
        StringBuilder SB = new StringBuilder();
       string Line = File.ReadAllText(@"C:\temp\ArcId.txt");


         //if (Line.Contains("Toolbar()")> 0)


        foreach (string Toolbar() in Line)
    {

        string [] words = Line.split(sep);
        string Line = words[3].Replace("()"," ");
    }
       SB.Appendline("ToolBarCollection.Add Arcid.");

File.WriteAllText(@"C:\temp\IDs.txt",SB.ToString);

    }

    }
Posted
Comments
Patrice T 28-Oct-15 20:44pm    
Start by helping yourself and tell us what error messages and where.
Try to explain what the code is supposed to do.
phil.o 28-Oct-15 21:03pm    
Why everyone seems to think that the exact error message, as well as the place where the error occured, are not relevant facts? :)
BillWoodruff 28-Oct-15 23:16pm    
imho anyone who hasn't learned to set break-points and isolate where errors are occurring is ... uninformed

1 solution

It seems like you have mixed up ReadAllText and ReadAllLines.
You can use either or, but the in first case you get all text in one string and cannot perform a foreach loop on string level. ReadAllLines on the other hand will return an array of strings, one string for each line in the file.

If the file is relatively small you can use ReadAllText and do the search and replace on the single string like
C#
string inputText = File.ReadAllText(@"C:\temp\ArcId.txt");
string outputText = inputText.Replace("Toolbar()", "Toolbar");
File.WriteAllText(@"C:\temp\IDs.txt", outputText);

Not sure that this replacement is exactly what you want to do, but you didn't give that information.

You can also read line by line:
C#
StringBuilder outputText = new StringBuilder();
foreach (string line in File.ReadAllLines(@"C:\temp\ArcId.txt"))
{
    outputText.Append(line.Replace("Toolbar()", "Toolbar"));
}
File.WriteAllText(@"C:\temp\IDs.txt", outputText.ToString());
 
Share this answer
 
Comments
BillWoodruff 28-Oct-15 23:15pm    
+5
George Jonsson 29-Oct-15 3:39am    
Thanks Bill.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900