save to textfile

  • Thread starter Thread starter Maarten
  • Start date Start date
M

Maarten

hi all when i do this:

Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem3.Click

For inti = 1 To 16

Call SaveTextToFile("Channel" & " " & inti & " = " & dchanPos(inti),
"C:\Documents and Settings\Maarten\My Documents\PLC.uni", "Error")

Next

End Sub



it only saves the last loop so in the textfile stands Channel 16 = False

but i want al lines (16) are saved in the file

thanks Maarten
 
Hi,

Need to see code for SaveTextToFile to figure out problem.

Ken
---------------
hi all when i do this:

Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem3.Click

For inti = 1 To 16

Call SaveTextToFile("Channel" & " " & inti & " = " & dchanPos(inti),
"C:\Documents and Settings\Maarten\My Documents\PLC.uni", "Error")

Next

End Sub



it only saves the last loop so in the textfile stands Channel 16 = False

but i want al lines (16) are saved in the file

thanks Maarten
 
Public Function SaveTextToFile(ByVal strData As String, _

ByVal FullPath As String, _

Optional ByVal ErrInfo As String = "") As Boolean

Dim Contents As String

Dim bAns As Boolean = False

Dim objReader As StreamWriter

Try

objReader = New StreamWriter(FullPath)

objReader.Write(strData)



objReader.Close()

bAns = True

Catch Ex As Exception

ErrInfo = Ex.Message

End Try

Return bAns

End Function

thanks Maarten
 
Hi,

You are creating a new file each time you call savetexttofile and
writing one line to it. Try something like this.

Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem3.Click

Dim objReader As StreamWriter

Try

objReader = New StreamWriter(FullPath)

For inti = 1 To 16

objReader.Write("Channel" & " " & inti & " = " & dchanPos(inti),
"C:\Documents and Settings\Maarten\My Documents\PLC.uni")

Next

objReader.Close()


Catch Ex As Exception

ErrInfo = Ex.Message

End Try
End Sub


Ken
-----------------

Public Function SaveTextToFile(ByVal strData As String, _

ByVal FullPath As String, _

Optional ByVal ErrInfo As String = "") As Boolean

Dim Contents As String

Dim bAns As Boolean = False

Dim objReader As StreamWriter

Try

objReader = New StreamWriter(FullPath)

objReader.Write(strData)



objReader.Close()

bAns = True

Catch Ex As Exception

ErrInfo = Ex.Message

End Try

Return bAns

End Function

thanks Maarten
 
Maarten,

Most copied from Ken, however i thought he is overlooking something.

\\\
Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem3.Click
Dim myWriter As StreamWriter
Try
myWriter = New StreamWriter( "C:\Documents and Settings\Maarten\My
Documents\PLC.uni"))
For inti = 1 To 16
myWriter.Writeline("Channel" & " " & inti & " = " & dchanPos(inti))
Next
myWriter.Close()
Catch Ex As Exception
ErrInfo = Ex.Message
End Try
End Sub
///

Cor
 
Maarten,

Change:

objReader = New StreamWriter(FullPath)

to:

objReader = New StreamWriter(FullPath,True)

to append, rather than replace. Currently your code replace the text in the
file on each call to SaveTextToFile - that's why you only see the last line
saved.

For an explanation see:
http://msdn.microsoft.com/library/d.../frlrfSystemIOStreamWriterClassctorTopic4.asp

NOTE: Unless there is a special reason to have a separate method to open,
save, and close the file for each line to be written (SaveTextToFile), you
should do it all in one method for efficiency e.g. Open the file, write all
the lines, close the file instead of opening and closing the file over and
over again.
--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com
 
Hi,

Thanks it should have been new streamwritter

Ken
---------------
Maarten,

Most copied from Ken, however i thought he is overlooking something.

\\\
Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem3.Click
Dim myWriter As StreamWriter
Try
myWriter = New StreamWriter( "C:\Documents and Settings\Maarten\My
Documents\PLC.uni"))
For inti = 1 To 16
myWriter.Writeline("Channel" & " " & inti & " = " & dchanPos(inti))
Next
myWriter.Close()
Catch Ex As Exception
ErrInfo = Ex.Message
End Try
End Sub
///

Cor
 
Back
Top