Whats wrong with this code its adding characters to line

vbMarkO

Well-known member
Joined
Aug 19, 2005
Messages
157
Ok, I have followed the tutorial and have went back over the code but cant see anywhere where I might have deviated.

So why is it when it writes to the text that it takes this "Mark" and when I read it I get this -- Mark -- I getwhat was writen in the textbox plus what looks like a carriage return.....

I know the quotes I used above at the end arent exactly the same but it gives the idea.

Anyway, here is the code please tell me where I am going wrong.

Code:
Imports System.IO

Public Class Form1

    Private Sub btnOpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenFile.Click
        Dim sFileName As String = Application.StartupPath & "\VB.txt"
        Dim myFileStream As FileStream
        Try
            myFileStream = New FileStream(sFileName, _
        FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite)

        Catch ex As Exception
            Make sure that it exists, and if it doesnt display an error 
            MessageBox.Show("Count not open the file. Make sure " & sFileName & _
            " exists in the location shown.")
        Finally
            If myFileStream.CanRead Then
                 Now we read or verify file is open and read
                txtFileDisplay.Text = "File Opened"
                 Now we are done with the file so close it
                myFileStream.Close()
            End If
        End Try


    End Sub

    Private Sub btnReadFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadFile.Click
        Dim sFileName As String = Application.StartupPath & "\VB.txt"
        Dim myFileStream As New FileStream(sFileName, _
         FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read)
        Dim myReader As New StreamReader(myFileStream)
        Dim sFileContents As String = myReader.ReadToEnd()
        txtFileDisplay.Text = sFileContents
        myReader.Close()
        myFileStream.Close()


    End Sub

    Private Sub btnWriteFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteFile.Click
         First Declare those variables
        Dim sFileName As String = Application.StartupPath & "\VB.txt"
        Dim myFileStream As New FileStream(sFileName, _
         FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)
         Now Create the streamwriter
        Dim myWriter As New StreamWriter(myFileStream)
         Now Write what is contained within the textbox
        myWriter.WriteLine(txtFileDisplay.Text)
         Flush before we close
        myWriter.Flush()
        Close everything
        myWriter.Close()
        myFileStream.Close()
         Lets Clear the text
        txtFileDisplay.Text = ""

    End Sub
End Class
 
DUH,

Never mind this is resolved I figured it out and its not the code its the textbox multiline was set to false...

it now works
 
Back
Top