I wrote a method that will go through all text files, replace text, and update a textbox with said changes. It works after I run it a first time, but subsequent executions seem to infer that the files weren't changed the first time.
private void changeText(string searchString, string newString, FileInfo[] listOfFiles)
{
foreach (FileInfo tempfi in listOfFiles)//Foreach File
{
string fileToBeEdited = tempfi.FullName;
File.SetAttributes(fileToBeEdited, File.GetAttributes(fileToBeEdited) & ~FileAttributes.ReadOnly); //Remove ReadOnly Property
string strFile = System.IO.File.ReadAllText(fileToBeEdited); //Reads In Text File
if(strFile.Contains(newString))//If the replacement string is contained in the text file
{
strFile = strFile.Replace(searchString, newString);
System.IO.File.WriteAllText(fileToBeEdited, strFile); //Write changes to File
myTextBox.Text = "File Changed: " + fileTobeEdited.ToString() + Environment.NewLine; //Notify User
}
}
}
If I run this 1 time or 100 times my text files are updated just fine. If I run this a second time my textbox is re-updated saying that it updated the new files.
I would expect that this method wouldn't find any text to replace after running it a first time.