I'm trying to create a .txt file that will hold a handful of numbers. I want to be able to add them, then retrieve the sum with my program (or retrieve the numbers and add them via the program, whatever). Unfortunately, I haven't a clue how to accomplish this. Also, I'm not positive that what I have is correct so far. This is where I'm at atm:

Private Sub submitBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
   Handles submitBtn.Click

      Dim runningTally As DialogResult = OpenFileDialog.ShowDialog
      runningTally = Val(timeSpentTxt.Text)
      'runningTally = runningTally + timeSpent 
      If runningTally = Windows.Forms.DialogResult.Cancel Then
         Return
      End If
      output = New StreamWriter(fileName, True)
      ouput.WriteLine(timeSpentTxt.Text)
      'lifetime = lifetime + runningTally

   End Sub

using system.io to write/read file. put in top of code.
try this following code :

Imports System.IO
...
    
    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
    
    Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click
        SaveTextToFile(TextBox1.Text, "D:\Jx_Man2.txt")
    End Sub
End Class

The file is being created and it is somewhat working. It works okay if you have the program open and enter like 3, 4, 1. It's supposed to subtract these from 10. It does so, saying that you have 2 hours left. But once it's shut down and you check the file, it is only holding the last number entered, not all of them. This leave me with a few questions:

How can I make the file hold all of the values, even after the program is shut down?

I need to make a Reset button to clear the file out. How do I make this happen?

Here's this section of code so far:

Private Sub submitBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
   Handles submitBtn.Click

      SaveTextToFile(timeSpentTxt.Text, "C:\Documents and Settings\All Users\Documents\College-C\IT\VB.Net\Assignments\Semester Project\ATV Records\runningTally.txt")

      'var declarations
      Dim todaysTime As Decimal = Val(timeSpentTxt.Text)
      runningTally += todaysTime
      timeRemaining = 10 - runningTally
      timeRemainingOutputLbl.Text = timeRemaining
      lifetime += runningTally
      lifetimeHrsOutputLbl.Text = lifetime

   End Sub

(Some of the variables were previously declared as Public.)

This article has been dead for over six months. Start a new discussion instead.