When Default is not the default
-
I spent over an hour yesterday trying to figure out why reading in an encrypted configuration file created in another program produced the wrong results. First try looked like this:
Using sr As New IO.StreamReader(strPath)
which didn't work, so I tried the next overload that takes an encoding parameter. I tried all of them with no joy until I tried this one:
Using sr As New IO.StreamReader(strPath, System.Text.Encoding.Default)
which worked perfectly. :wtf:
"Go forth into the source" - Neal Morse
-
I spent over an hour yesterday trying to figure out why reading in an encrypted configuration file created in another program produced the wrong results. First try looked like this:
Using sr As New IO.StreamReader(strPath)
which didn't work, so I tried the next overload that takes an encoding parameter. I tried all of them with no joy until I tried this one:
Using sr As New IO.StreamReader(strPath, System.Text.Encoding.Default)
which worked perfectly. :wtf:
"Go forth into the source" - Neal Morse
The default behaviour is to attempt to detect the encoding based on the Byte Order Mark[^] within the stream's content. If no BOM is found, it falls back to UTF8. It's mentioned in the remarks on the class:
StreamReader defaults to UTF-8 encoding unless specified otherwise, instead of defaulting to the ANSI code page for the current system. UTF-8 handles Unicode characters correctly and provides consistent results on localized versions of the operating system. If you get the current character encoding using the CurrentEncoding property, the value is not reliable until after the first Read method, since encoding auto detection is not done until the first call to a Read method.
And on the constructor:
StreamReader Constructor (String) - MSDN[^]
This constructor initializes the encoding to UTF8Encoding and the buffer size to 1024 bytes.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
I spent over an hour yesterday trying to figure out why reading in an encrypted configuration file created in another program produced the wrong results. First try looked like this:
Using sr As New IO.StreamReader(strPath)
which didn't work, so I tried the next overload that takes an encoding parameter. I tried all of them with no joy until I tried this one:
Using sr As New IO.StreamReader(strPath, System.Text.Encoding.Default)
which worked perfectly. :wtf:
"Go forth into the source" - Neal Morse
You probably shouldn't use my initials in your code like that ;p
Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.
Simplicity is prerequisite for reliability. — Edsger W. Dijkstra
Regards, Sander