简体   繁体   中英

Replacing Text in File

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.

The variable fileToBeEdited was not initialized.

You have to look for files that contain searchString not newString !

private void changeText(string searchString, string newString, FileInfo[] listOfFiles)
{
    foreach (FileInfo tempfi in listOfFiles) {
        string fileToBeEdited = tempfi.FullName; // <== This line was missing
        File.SetAttributes(tempfi.FullName, File.GetAttributes(fileToBeEdited) &
                                            ~FileAttributes.ReadOnly);
        string strFile = System.IO.File.ReadAllText(fileToBeEdited);
        if (strFile.Contains(searchString)) { // <== replaced newString by searchString
            strFile = strFile.Replace(searchString, newString);
            System.IO.File.WriteAllText(fileToBeEdited, strFile);
            myTextBox.Text = "File Changed: " + fileToBeEdited.ToString() +
                             Environment.NewLine;
        }
    }
}

It doesn't look like you are actually changing the file. You are checking to see if a string is contained in the file and then if it is, you write that file back. You would have to do something like this:

    private void changeText(string searchString, string newString, FileInfo[] listOfFiles)
{
    foreach (FileInfo tempfi in listOfFiles)//Foreach File
    {
        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); // make the changes
            System.IO.File.WriteAllText(fileToBeEdited, strFile); //Write changes to File
            myTextBox.Text = "File Changed: " + fileTobeEdited.ToString() + Environment.NewLine; //Notify User
        }
    }
}

Then you will be able to actually save the changes to the file and after the first run the new file will have been written.

Maybe I'm misreading the code, but you appear to be missing the replace!

    string strFile = System.IO.File.ReadAllText(fileToBeEdited); //Reads In Text File
    if(strFile.Contains(searchString))//If the replacement string is contained in the text file
    {
        strFile = strFile.Replace(searchString, newString);
 ....

Also note how I check if the file contains the searchstring, not the newstring.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM