Currently I have some code to replace strings in a file that looks like this:
File.WriteAllText(filePath, Regex.Replace(File.ReadAllText(filePath),
"( " + column.Key + " )",
" " + column.Value + " "
));
File.WriteAllText(filePath, Regex.Replace(File.ReadAllText(filePath),
"(\\[\"" + column.Key + "\"\\])",
"[\"" + column.Value + "\"]"
));
However, each replacement opens and closes the file, and it seems that occasionally they run "too fast" and one replacement will not work because the file did not close yet in a previous string replacement. Is there any code I can reuse that solves this issue, perhaps using the FileStream class (so I can open and close once)? Or suggestions on a better way to do this? Just wondering if there is something simpler I can do than having to create byte arrays of the strings I want to replace and writing code to read, write, and seek through bytes manually. Thanks.