C# - File Encoding Problem.

Posted by user301330 on Stack Overflow See other posts from Stack Overflow or by user301330
Published on 2010-03-25T21:20:53Z Indexed on 2010/03/25 21:23 UTC
Read the original article Hit count: 372

Filed under:
|
|

Hello,

I'm have a StringBuilder that is writing content to a file. Towards the end of each file, I'm writing the copyright symbol. Oddly, I have noticed that whenever the copyright symbol is written, it is preceeded by a "Â". My code that generates the content of the file looks like this:

using (StringWriter stringWriter = new StringWriter())
{
  stringWriter = GetFileContent();

  string targetPath = ConfigurationManager.AppSettings["TargetPath"];
  using (StreamWriter streamWriter = new StreamWriter(targetPath, false))
  {
    StringBuilder sb = new StringBuilder(stringWriter.ToString());

    // Attempted fix
    string content = sb.ToString();
    content = content.Replace("Â", "");

    streamWriter.Write(content);
  }
}

As you can tell, I tried to do a find-and-replace. In the process, I noticed that a "Â" was not in the content itself. This makes me believe there is something occurring in the streamWriter. However, I'm not sure what it could be.

Can someone please tell me why a "Â" would be popping up before the "©" symbol and how to fix it? I believe it has something to do with encoding, but I'm not sure

Thank you!

© Stack Overflow or respective owner

Related posts about c#

Related posts about encoding