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
 
Maarten,

How do you open the file in you SaveToTextFile method?

Check out the File Class's AppendText method.


--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com
 
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
 
Ken,

Thanks it should have been new streamwritter

Thanks as well for that extra addition however with a lot of messages we
come together to a result.

:-)

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

only integer 38
count down multiple textboxes 2
Debugging Error 1
dim test as new form1 28
Pointer in API making life hell 3
Does a menu item lose dataset information? 2
closing 1
datagrid add checkboxes 4

Back
Top