Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. When Default is not the default

When Default is not the default

Scheduled Pinned Locked Moved The Weird and The Wonderful
workspace
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kmoorevs
    wrote on last edited by
    #1

    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

    Richard DeemingR Sander RosselS 2 Replies Last reply
    0
    • K kmoorevs

      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

      Richard DeemingR Online
      Richard DeemingR Online
      Richard Deeming
      wrote on last edited by
      #2

      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 Class - MSDN[^]

      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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      • K kmoorevs

        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

        Sander RosselS Offline
        Sander RosselS Offline
        Sander Rossel
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups