<body><script type="text/javascript"> function setAttributeOnload(object, attribute, val) { if(window.addEventListener) { window.addEventListener('load', function(){ object[attribute] = val; }, false); } else { window.attachEvent('onload', function(){ object[attribute] = val; }); } } </script> <div id="navbar-iframe-container"></div> <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> <script type="text/javascript"> gapi.load("gapi.iframes:gapi.iframes.style.bubble", function() { if (gapi.iframes && gapi.iframes.getContext) { gapi.iframes.getContext().openChild({ url: 'https://www.blogger.com/navbar.g?targetBlogID\x3d6566853\x26blogName\x3d1%25+inspiration\x26publishMode\x3dPUBLISH_MODE_BLOGSPOT\x26navbarType\x3dBLUE\x26layoutType\x3dCLASSIC\x26searchRoot\x3dhttp://patke.blogspot.com/search\x26blogLocale\x3den\x26v\x3d2\x26homepageUrl\x3dhttp://patke.blogspot.com/\x26vt\x3d8220196945898414734', where: document.getElementById("navbar-iframe-container"), id: "navbar-iframe" }); } }); </script>
Archives
Subscribe


Wednesday, March 31, 2004

Up late last night trying to figure out a silly problem with reading text files. Turned out to be more silly then I thought, but I wanted to post the solution on the web one more time...

Problem: Trying to read "special" characters - like ©.

Solution: DotNet StreamReader defaults to UTF8. Another encoding is needed. Ultimately, the appropriate encoding for me was "iso-8859-1".

So...

//Read
using (StreamReader sr = new StreamReader(fileLocation,
System.Text.Encoding.GetEncoding("iso-8859-1"), false))
{
temp = sr.ReadToEnd();
sr.Close();
}

//Write
using (StreamWriter sw = new StreamWriter(fileLocation, false,
System.Text.Encoding.GetEncoding("iso-8859-1")))
{
sw.Write(msg);
sw.Flush();
sw.Close();
}

...Ultimately, my problem was that I was using one encoding to read and another encoding to write *duh*! Like I say - not rocket science - but maybe it will help someone. ...maybe I was up a little too late.

Permalink
Comments: Post a Comment