Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9028111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:54:27+00:00 2026-06-16T06:54:27+00:00

I wrote a method that will go through all text files, replace text, and

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T06:54:29+00:00Added an answer on June 16, 2026 at 6:54 am

    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;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write an Audit Log method that will log all changes
I'm trying to write a unit test that will loop through all action methods
I'm trying to write a method that will compute all permutations of a power
What I'm trying to do is write a method that will return all of
All, I am trying to write a cross platform (hence: boost) method/function that will
I am trying to write code that will loop through all cells in a
I'm trying to write an extension method for objects that will dump the structure
I wrote a method that extracts fields from an object like this: private static
I have and old(ish) C# method I wrote that takes a number and converts
I wrote a windows service in C# that does some functionality (a public method)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.