I currently have code for replacing lines 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 sometimes they start “too fast”, and one replacement will not work, because the file has not closed yet with the previous line replacement. Is there any code that I can reuse that solves this problem, possibly using the FileStream class (so that I can open and close once)? Or suggestions for a better way to do this? Just wondering if there is anything simpler than creating byte arrays of strings that I want to replace and writing code to read, write and search bytes manually. Thank.
source
share