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

  • SEARCH
  • Home
  • 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 3693352
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:22:03+00:00 2026-05-19T04:22:03+00:00

I have written a simple utility that loops through all C# files in my

  • 0

I have written a simple utility that loops through all C# files in my project and updates the copyright text at the top.

For example, a file may look like this;

//Copyright My Company, © 2009-2010

The program should update the text to look like this;

//Copyright My Company, © 2009-2010

However, the code I have written results in this;

//Copyright My Company, � 2009-2011

Here is the code I am using;

public bool ModifyFile(string filePath, List<string> targetText, string replacementText)
{
    if (!File.Exists(filePath)) return false;
    if (targetText == null || targetText.Count == 0) return false;
    if (string.IsNullOrEmpty(replacementText)) return false;

    string modifiedFileContent = string.Empty;
    bool hasContentChanged = false;

    //Read in the file content
    using (StreamReader reader = File.OpenText(filePath))
    {
        string file = reader.ReadToEnd();

        //Replace any target text with the replacement text
        foreach (string text in targetText)
            modifiedFileContent = file.Replace(text, replacementText);

        if (!file.Equals(modifiedFileContent))
            hasContentChanged = true;
    }

    //If we haven't modified the file, dont bother saving it
    if (!hasContentChanged) return false;

    //Write the modifications back to the file
    using (StreamWriter writer = new StreamWriter(filePath))
    {
        writer.Write(modifiedFileContent);
    }

    return true;
}

Any help/suggestions are appreciated. Thanks!

  • 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-05-19T04:22:03+00:00Added an answer on May 19, 2026 at 4:22 am

    This is an encoing problem.

    I think you should change this line

    using (StreamWriter writer = new StreamWriter(filePath))
    

    To a variant that saves with the correct encoding (the overload that looks like this)

    using (StreamWriter writer = new StreamWriter(filePath, false, myEncoding))
    

    To get the correct encoding, where you have opened the file add this line

    myEncoding = reader.CurrentEncoding;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple single threaded utility written in C# that inserts data into
I have written a simple jQuery script that changes the hash tag of a
I have a hobby project that is written in C# using MonoDevelop. I've been
I have written this simple C program that changes the rotation file name. Eg:
I have four small single-form utility applications that I have written in Delphi (Win32),
I have written a simple program that does autocorrelation as follows...I've used pgi accelerator
I have written a simple program that use some classes and procol buffers. These
I have written a simple jQuery dialog box that will appear in an asp
I have written a simple MSN -style program that will send and retrieve messages
Below, I have written simple code that re-creates problems that have emerged as my

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.