Results 1 to 18 of 18

Thread: VS2012 .NET 4.5 - StreamWriter produces Garbage

  1. #1

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    VS2012 .NET 4.5 - StreamWriter produces Garbage

    I very rarely write to text files, but have encountered a situation where I cannot avoid it. I am using a StreamWriter to write lines of text to a text file. Here is a sample of what I have:


    vb.net Code:
    1. Dim WriteThisInfo As String = "12345678901234567890"
    2.  
    3. Using myStream As StreamWriter = New StreamWriter("Sample.txt")
    4.  
    5.      myStream.WriteLine(WriteThisInfo)
    6.  
    7. End Using


    When I open the text file, this is what I see:

    †††〲㌱㘰㈰㘱㠱〲〲㌱㘰㈰㘱㠱ㄴ‰††‰††‰††‰††‰††‰††‰††‰††‰† â€ â€°â€ â€ â€°â€ â€ â€°â€ â€ â€°â€ â€ â€°â€ â€ â€°â€ â€ â€°â€ â€ â€°â€ â€ â€°â€ â€ â€°


    Any ideas?
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  2. #2

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: VS2012 .NET 4.5 - StreamWriter produces Garbage

    If it helps at all, I am actually writing fields, so it is actually a little closer to this:

    vb.net Code:
    1. Dim string1 As String = "123"
    2. Dim string2 As String = "789"
    3. Dim WriteThisInfo As String = string1.PadRight(6) & string2.PadRight(6)
    4.  
    5. Using myStream As StreamWriter = New StreamWriter("Sample.txt")
    6.     myStream.WriteLine(WriteThisInfo)
    7. End Using
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: VS2012 .NET 4.5 - StreamWriter produces Garbage

    my guess is that your PC's default language isn't English. right?
    try using an encoding option:

    Code:
    Dim WriteThisInfo As String = "12345678901234567890"
    
    Using myStream As New IO.StreamWriter("Sample.txt", False, System.Text.Encoding.Default)
        myStream.WriteLine(WriteThisInfo)
    End Using

  4. #4

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: VS2012 .NET 4.5 - StreamWriter produces Garbage

    Quote Originally Posted by .paul. View Post
    my guess is that your PC's default language isn't English. right?
    No, it's definitely English. I tried going down that path too, so I have tested using Encoding.Default and Encoding.ASCII but still got the same results. I don't see how this can happen when using different encoding types.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: VS2012 .NET 4.5 - StreamWriter produces Garbage

    what are you using to open the file?

  6. #6

    Re: VS2012 .NET 4.5 - StreamWriter produces Garbage

    Try UTF-8 as the encoding. All I can think of is that somehow, the CultureInfo of your PC is messing things up, but that's all I've got.

  7. #7

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: VS2012 .NET 4.5 - StreamWriter produces Garbage

    Quote Originally Posted by .paul. View Post
    what are you using to open the file?
    The file doesn't exist, so StreamWriter creates it at runtime.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  8. #8

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: VS2012 .NET 4.5 - StreamWriter produces Garbage

    Quote Originally Posted by formlesstree4 View Post
    Try UTF-8 as the encoding. All I can think of is that somehow, the CultureInfo of your PC is messing things up, but that's all I've got.
    The file I am creating will be read into another system, and the system specs require a flat ASCII file (no unprintable characters)
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: VS2012 .NET 4.5 - StreamWriter produces Garbage

    Quote Originally Posted by circuits2 View Post
    Quote Originally Posted by .paul. View Post
    what are you using to open the file?
    The file doesn't exist, so StreamWriter creates it at runtime.
    no... what are you using to open the file to verify what was written?

  10. #10

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: VS2012 .NET 4.5 - StreamWriter produces Garbage

    Quote Originally Posted by .paul. View Post
    no... what are you using to open the file to verify what was written?
    Sorry, opening in Notepad.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  11. #11

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: VS2012 .NET 4.5 - StreamWriter produces Garbage

    Quote Originally Posted by .paul. View Post
    no... what are you using to open the file to verify what was written?
    Strangely enough, if I open in WordPad it looks normal.........?
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  12. #12
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: VS2012 .NET 4.5 - StreamWriter produces Garbage

    You broke Notepad? How's that even possible?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  13. #13

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: VS2012 .NET 4.5 - StreamWriter produces Garbage

    Quote Originally Posted by dunfiddlin View Post
    You broke Notepad? How's that even possible?
    Yes, I seem to be stuck in the notepad black hole of encoding problems.

    What I don't understand, is the default encoding is UTF-8, which notepad should read just fine, but doesn't want to for me. I guess I made it mad.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  14. #14
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: VS2012 .NET 4.5 - StreamWriter produces Garbage

    This is curious. Could you attach the text file that's pwning Notepad ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  15. #15

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: VS2012 .NET 4.5 - StreamWriter produces Garbage

    Here you go Niya!
    Attached Files Attached Files
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  16. #16

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: VS2012 .NET 4.5 - StreamWriter produces Garbage

    Just noticed something else that is weird. The default encoding is UTF-8. I have tried using Encoding.Default, which produced the results previously stated. However, if I use Encoding.UTF8, it actually displays in notepad correctly. Now if I could just figure out why it will not display ASCII.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  17. #17

    Re: VS2012 .NET 4.5 - StreamWriter produces Garbage

    Fun fact: If I open the file in Notepad as ANSI, instead of Unicode (my system defaults to Unicode) I get the correct text.

  18. #18
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: VS2012 .NET 4.5 - StreamWriter produces Garbage

    Quote Originally Posted by formlesstree4 View Post
    Fun fact: If I open the file in Notepad as ANSI, instead of Unicode (my system defaults to Unicode) I get the correct text.
    What in God's name .....I have to open it as UTF-8 for it to work correctly. Any other encoding produces garbage, even ANSI. This is really strange indeed.

    [EDIT]

    Wow, now it just took a turn into Twilight Zone strange....It opens correctly some of the time in NotePad using UTF-8. I really don't know where to start on figuring this out. Unicode is one strange beast indeed.
    Last edited by Niya; Jun 10th, 2013 at 03:42 AM.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width