Creating text files containing chr(13) and chr(10)

mol

New member
Joined
Jul 15, 2010
Messages
1
Programming Experience
Beginner
I am a beginner in vb 10 (vb.net).
I am trying to create a text file "something.txt" and write a long string
to the file. My string contains formatting chars newline and carriage return, which I want to keep.

I tried:
FileToSave.Write("test" & Chr(10) & Chr(13) & "test2", RichTextBoxStreamType.RichText)

using the System.IO.StreamWriter method but the line feeds and carriage returns are lost and all the text is just streamed together with no formatting.

Where 'FileToSave' is the path and name of the file.
Note: I know I can use the vbnewline instead of chr(x) but this is not the issue.

Can anyone tell me what I am doing wring please?

regards
mol
 
I'm afraid that your code doesn't really make sense. You say that 'FileToSave' is the path and name of the file, but that would make it a String and the String class doesn't have a Write method. More likely it's a StreamWriter, but the StreamWriter.Write method has no overload that takes a RichTextBoxStreamType value as an argument. The only place that type is used is in the LoadFile and SaveFile methods of the RichTextBox control, so you can get rid of that. Next, a line break is a carriage return followed by a line feed, which is 13 followed by 10, not the other way around. That's just one reason that using ASCII codes is a bad idea.

If you have a StreamWriter and you want to write two lines to the file it has open then the logical option would be:
VB.NET:
myStreamWriter.WriteLine("line1")
myStreamWriter.Write("line2")
Alternatively, you could do this:
VB.NET:
myStreamWriter.Write("line1" & Environment.NewLine & "line2")
There's really no reason to do anything other than one of those two.
 
Back
Top