Gabe Gabe - 2 months ago 6
C# Question

Open a file and replace strings in C#

I'm trying to figure out the best way to open an existing file and replace all strings that match a declared string with a new string, save it then close.

Suggestions ?

Answer

Can be done in one line:

File.WriteAllText("Path", Regex.Replace(File.ReadAllText("Path"), "[Pattern]", "Replacement"));
Comments