0

I need to find a section header, in this case "[Store Hours]", in a text file that I'm using to save the settings for the program. I need to skip the header and replace the next 7 lines with the text that is in the text boxes. The code below currently deletes the header "[Store Hours]" and does not replace any of the lines.

 Dim objFileName As String = "Settings.txt"

    Private Sub BtnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnSaveHours.Click
        Dim OutPutLine As New List(Of String)()
        Dim matchFound As Boolean
        For Each line As String In System.IO.File.ReadAllLines(objFileName)
            matchFound = line.Contains("[Store Hours]")
            If matchFound Then

                'does not skip the header line
                line.Skip(line.Length)

                'Need to loop through this 7 times (for each day of the week)
                'without reading the header again
                For intCount = 0 To 6
                    Dim aryLabelDay() As String = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
                    Dim varLabelNameIn As String = "txt" & aryLabelDay(intCount).ToString & "In"
                    Dim varDropNameIn As String = "drp" & aryLabelDay(intCount).ToString & "In"
                    Dim varLabelNameOut As String = "txt" & aryLabelDay(intCount).ToString & "Out"
                    Dim varDropNameOut As String = "drp" & aryLabelDay(intCount).ToString & "Out"
                    Dim varTextBoxInControl() As Control = Me.Controls.Find(varLabelNameIn, True)
                    Dim varDropBoxInControl() As Control = Me.Controls.Find(varDropNameIn, True)
                    Dim varTextBoxOutControl() As Control = Me.Controls.Find(varLabelNameOut, True)
                    Dim varDropBoxOutControl() As Control = Me.Controls.Find(varDropNameOut, True)
                    Dim dymTextNameIn As TextBox = DirectCast(varTextBoxInControl(0), TextBox)
                    Dim dymDropNameIn As ComboBox = DirectCast(varDropBoxInControl(0), ComboBox)
                    Dim dymTextNameOut As TextBox = DirectCast(varTextBoxOutControl(0), TextBox)
                    Dim dymDropNameOut As ComboBox = DirectCast(varDropBoxOutControl(0), ComboBox)
                    Dim ReplaceLine As String
                    ReplaceLine = dymTextNameIn.Text & "," & dymDropNameIn.Text & "," & dymTextNameOut.Text & "," & dymDropNameOut.Text

                    'this doesn't replace anything
                    line.Replace(line, ReplaceLine)
                    intCount += 1
                Next intCount
            Else
                OutPutLine.Add(line)
            End If
        Next
    End Sub
BrianC987
  • 167
  • 1
  • 14
  • 2
    The `line.Skip(line.Length)` line is doing nothing at all. I'm not sure what you think that is doing, but I assure you, it's not. – Steven Doggart Dec 05 '13 at 20:44

3 Answers3

1

Instead of using ReadAllLines simply use a streamreader and read the file line by line, like this;

Dim line As String
Using reader As StreamReader = New StreamReader("file.txt")
        ' Read one line from file
         line = reader.ReadLine
         If(line.Contains("[Store Hours]") Then
             'The current line is the store hours header, so we skip it (read the next line)         
              line = reader.ReadLine
              'Process the line like you want, and keep processing through the lines by doing a readline each time you want to progress to the next line.
         End If
End Using

More importantly though, you should not be saving the settings for your program in a text file. They should be stored in app.config or web.config. See this question for further guidance on that.

Community
  • 1
  • 1
n00b
  • 4,341
  • 5
  • 31
  • 57
  • App/web.config requires a lot more overhead to read from and isn't a good place to store business data. It should really only be used for *application* data, related to application settings. Web.config files should be encrypted, making it very difficult to change them. – JDB Dec 05 '13 at 20:56
0

Part of your confusion might be coming from the fact that you can't just replace part of a text file without copying it and overwriting it. One way, to do this, is to copy the file to memory changing the appropriate lines and overwriting the existing file with the new information. Here's one way that can be done:

Private Sub BtnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnSaveHours.Click
    Dim OutPutLine As New List(Of String)()
    Dim sr As New StreamReader(objFileName)
    While Not sr.EndOfStream
        Dim line = sr.ReadLine
     'Found the header so let's save that line to memory and add all the other _
      info after it.
        If line.Contains("[Store Hours]") Then
            OutPutLine.Add(line)
            'Need to loop through this 7 times (for each day of the week)
            'without reading the header again
            Dim aryLabelDay() As String = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
            For intCount = 0 To 6
                'even though we're not using the info from the file, calling _
                 Readline advances the file pointer so that the next iteration _
                 of the while loop we can finish reading the file.
                If Not sr.EndOfStream Then
                    line = sr.ReadLine
                    Dim dymTextNameIn As TextBox = DirectCast(Me.Controls("txt" & aryLabelDay(intCount) & "In"), TextBox)
                    Dim dymDropNameIn As ComboBox = DirectCast(Me.Controls("drp" & aryLabelDay(intCount) & "In"), ComboBox)
                    Dim dymTextNameOut As TextBox = DirectCast(Me.Controls("txt" & aryLabelDay(intCount) & "Out"), TextBox)
                    Dim dymDropNameOut As ComboBox = DirectCast(Me.Controls("drp" & aryLabelDay(intCount) & "Out"), ComboBox)
                    OutPutLine.Add(dymTextNameIn.Text & "," & dymDropNameIn.Text & "," & dymTextNameOut.Text & "," & dymDropNameOut.Text)
                End If
            Next
        Else
        'Any line that isn't in that section gets copied as is.
            OutPutLine.Add(line)
        End If
    End While
    'Copy all the new info to the same file overwriting the old info.
    File.WriteAllLines(objFileName, OutPutLine)
End Sub

On a side note. The Controls collection is indexed by number or name which makes it fairly simple to access the appropriate control just by knowing its name.

tinstaafl
  • 6,908
  • 2
  • 15
  • 22
0

Thanks for the help I actually figured it out and this is the final code I used

Private Sub btnSaveHours_Click(sender As System.Object, e As System.EventArgs) Handles btnSaveHours.Click
        Dim intOutPutLine As New List(Of String)()
        Dim blnSearchString As Boolean
        Dim intLineCount As Integer = -1
        Dim intLoopCount As Integer
        For Each line As String In System.IO.File.ReadAllLines(objFileName)
            blnSearchString = line.Contains("[Store Hours]")
            If blnSearchString Then
                intLineCount = intOutPutLine.Count
                line.Remove(0)
                intLoopCount = 0
            ElseIf intLineCount = intOutPutLine.Count And intLoopCount < 7 Then
                line.Length.ToString()
                line.Remove(0)
                intLoopCount += 1
            Else
                intOutPutLine.Add(line)
            End If
        Next
        System.IO.File.WriteAllLines(objFileName, intOutPutLine.ToArray())
        Dim objFileWrite As New StreamWriter(objFileName, True)
        If File.Exists(objFileName) Then
            objFileWrite.WriteLine("[Store Hours]")
            Dim varMe As Control = Me
            Call subConvertFrom12to24Hours(objFileWrite, varMe)
        Else
            objFileWrite.WriteLine("[Store Hours]")
            Dim varMe As Control = Me
            Call subConvertFrom12to24Hours(objFileWrite, varMe)
        End If
        Call btnClear_Click(sender, e)
    End Sub
BrianC987
  • 167
  • 1
  • 14