I am trying to open a .txt file, search for one word and replace it with another everywhere it appears. I can do this but not with .txt file only with string’s that I wrote myself in the .cs file.Here is the method I have so far:
public void EditorialControl(string fileName, string word, string replacement)
{
List<string> list = new List<string>();
using (StreamReader reader = new StreamReader(directory + fileName))
{
string line;
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
list.Add(line);
}
reader.Close();
}
}
When I call the method in the Main(), it should take the parameters word and replace it with a replacement word of my choice.
Can you guys help me with the code to replace a word in the method?
It is way simpler than you might think:
And that’s it!