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:
Dim WriteThisInfo As String = "12345678901234567890"
Using myStream As StreamWriter = New StreamWriter("Sample.txt")
myStream.WriteLine(WriteThisInfo)
End Using
When I open the text file, this is what I see:
†††〲㌱㘰㈰㘱㠱〲〲㌱㘰㈰㘱㠱ㄴ‰††‰††‰††‰††‰††‰††‰††‰††‰� �†‰††‰††‰††‰††‰††‰††‰††‰††‰††‰
Any ideas?
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:
Dim string1 As String = "123"
Dim string2 As String = "789"
Dim WriteThisInfo As String = string1.PadRight(6) & string2.PadRight(6)
Using myStream As StreamWriter = New StreamWriter("Sample.txt")
myStream.WriteLine(WriteThisInfo)
End Using
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
Re: VS2012 .NET 4.5 - StreamWriter produces Garbage
Quote:
Originally Posted by
.paul.
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.
Re: VS2012 .NET 4.5 - StreamWriter produces Garbage
what are you using to open the file?
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.
Re: VS2012 .NET 4.5 - StreamWriter produces Garbage
Quote:
Originally Posted by
.paul.
what are you using to open the file?
The file doesn't exist, so StreamWriter creates it at runtime.
Re: VS2012 .NET 4.5 - StreamWriter produces Garbage
Quote:
Originally Posted by
formlesstree4
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)
Re: VS2012 .NET 4.5 - StreamWriter produces Garbage
Quote:
Originally Posted by
circuits2
Quote:
Originally Posted by
.paul.
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?
Re: VS2012 .NET 4.5 - StreamWriter produces Garbage
Quote:
Originally Posted by
.paul.
no... what are you using to open the file to verify what was written?
Sorry, opening in Notepad.
Re: VS2012 .NET 4.5 - StreamWriter produces Garbage
Quote:
Originally Posted by
.paul.
no... what are you using to open the file to verify what was written?
Strangely enough, if I open in WordPad it looks normal.........?
Re: VS2012 .NET 4.5 - StreamWriter produces Garbage
:eek: You broke Notepad? How's that even possible?
Re: VS2012 .NET 4.5 - StreamWriter produces Garbage
Quote:
Originally Posted by
dunfiddlin
:eek: You broke Notepad? How's that even possible?
Yes, I seem to be stuck in the notepad black hole of encoding problems. :wave:
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.
Re: VS2012 .NET 4.5 - StreamWriter produces Garbage
This is curious. Could you attach the text file that's pwning Notepad ?
1 Attachment(s)
Re: VS2012 .NET 4.5 - StreamWriter produces Garbage
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.
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.
Re: VS2012 .NET 4.5 - StreamWriter produces Garbage
Quote:
Originally Posted by
formlesstree4
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 :confused:.....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.