[VB.NET] Problem with carriage returns when writing to text files.

Bob Burke

New member
Joined
Apr 5, 2004
Messages
4
Location
Dublin, Ireland
Code:
Private Sub butSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butSave.Click
        Dim save As New SaveFileDialog()
        save.Title = "Save Domain List As Text File"
        save.Filter = "Text files (*.txt)|*.txt"
        save.FilterIndex = 0
        save.RestoreDirectory = True
        If save.ShowDialog = DialogResult.OK Then
            Dim contents As String
            Dim FS As FileStream = save.OpenFile
            Dim SW As New StreamWriter(FS, System.Text.Encoding.ASCII)
            contents = txtResults.Text()
            contents.Replace(vbCrLf, System.Environment.NewLine)
            SW.Write(contents)
            SW.Close()
            FS.Close()
        End If
    End Sub

This the code I am using to write contents (which is the contents of a multiline textbox named txtResults) to a text file. When I view the textfile in Wordpad or Editplus it is fine but in Notepad all off the data is on one line and is seperated by glyphs... i.e. no carriage returns.

I really need to be able to view this in Notepad and Im also wondering why I cant? Can anyone give me any pointers or workarounds?

Thanks a lot.
~Bob
 
Same output as before, all of the data on one line seperated by glyphs.

EDIT: Eek!! I had been using a rich text box instead of a normal text box... I had originally used a rich text box instead of a normal text box because I couldnt figure out how to get a normal text box to accept multiple lines. Sorry guys!
 
Last edited by a moderator:
PlausiblyDamp said:
what happens if you remove the line
Code:
 contents.Replace(vbCrLf, System.Environment.NewLine)
...maybe its the same dumb mistake I made some times.. try this:

Code:
contents = contents.Replace(vbCrLf, System.Environment.NewLine)

Hope this helps!

Andreas
 
Back
Top