user448374
user448374

Reputation:

StreamReader not working as expected

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!

Upvotes: 0

Views: 2636

Answers (5)

ndtreviv
ndtreviv

Reputation: 3624

I'll bet it's related to the encoding of the file contents. Make sure you instantiate your StreamWriter with the correct encoding. ( http://msdn.microsoft.com/en-us/library/f5f5x7kt.aspx )

Upvotes: -1

m.edmondson
m.edmondson

Reputation: 30922

You have to specify the encoding

System.Text.Encoding.UTF8 should do the trick.

Once you've sorted it please promise me to read this.

Upvotes: -1

HABJAN
HABJAN

Reputation: 9338

Get the Encoding from reader and use it in writer.

Changed code:

public bool ModifyFile(string filePath, List 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;
    Encoding sourceEndocing = null;

    using (StreamReader reader = File.OpenText(filePath))
    {
        sourceEndocing = reader.CurrentEncoding;
        string file = reader.ReadToEnd();

        foreach (string text in targetText)
            modifiedFileContent = file.Replace(text, replacementText);

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

    if (!hasContentChanged) return false;

    using (StreamWriter writer = new StreamWriter(filePath, false, sourceEndocing))
    {
        writer.Write(modifiedFileContent);
    }

    return true;
}

Upvotes: 1

abatishchev
abatishchev

Reputation: 100358

Try to use

StreamWriter(string path, bool append, Encoding encoding)

i.e.

new StreamWriter(filePath, false, new UTF8Encoding())

Upvotes: 1

&#216;yvind Br&#229;then
&#216;yvind Br&#229;then

Reputation: 60744

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;

Upvotes: 2

Related Questions