vb.net readalllines fill form 替换某些行的编码建议

vb.net readalllines fill form replace certain lines coding advice

我想要完成的是读取一个文本文件并通过从第二个表单中填写文本来选择要修改的某些行。这是我当前使用的代码示例。发生的事情是我正在寻找以 718 开头的行,然后在该行之后的某处将有以 720 开头的行。我需要这两行来填写第二个表格。我能想到的唯一方法就是一直向该行添加 1 直到它到达我需要的行。我对此还是个新手,我确信有一种更简单的方法可以使用 Try 或 While 来做到这一点,但我不确定如何做。感谢任何帮助。

Dim lines() As String = File.ReadAllLines(tempsave)
For i As Integer = 0 To lines.Length - 1
If lines(i).StartsWith("718") Then
  If lines(i + 1).StartsWith("720") Then
       Dim array() As String = lines(i).Split("*"c, "~"c)
       Dim array2() As String = lines(i + 1).Split("*"c, "~"c)
       FormFill.TextBox1.Text = array(3)
       FormFill.TextBox2.Text = array(9)
       FormFill.ShowDialog()
       lines(i) = lines(i).Replace(array(3), FormFill.TextBox1.Text)
       lines(i + 1) = lines(i + 1).Replace(array(9), FormFill.TextBox2.Text)
  Else
       If lines(i + 2).StartsWith("720") Then
           Dim array() As String = lines(i).Split("*"c, "~"c)
           Dim array2() As String = lines(i + 2).Split("*"c, "~"c)
           FormFill.TextBox1.Text = array(3)
           FormFill.TextBox2.Text = array(9)
           FormFill.ShowDialog()
              lines(i) = lines(i).Replace(array2(3),FormFill.TextBox1.Text)
              lines(i + 2) = lines(i + 2).Replace(array(9), FormFill.TextBox2.Text)
       End If
   End If
End If
Next

示例数据:

Input:
123*test*test*test~
718*test*test*test~
543*test*test*test~
720*test*test*test~

Output:
123*test*test*test~
718*test*test*newdata~
543*test*test*test~
720*test*test*newdata~

在这里,试试这个:

Public Sub Lines()
  Dim _
    aNextLines,
    aAllLines As String()

  Dim _
    s718Line,
    s720Line As String

  aAllLines = IO.File.ReadAllLines("D:\Logs\Data.log")

  For i As Integer = 0 To aAllLines.Length - 1
    If aAllLines(i).StartsWith("718") Then
      s718Line = aAllLines(i)
      aNextLines = aAllLines.Skip(i + 1).ToArray
      s720Line = aNextLines.FirstOrDefault(Function(Line) Line.StartsWith("720"))

      ' Process data here

    End If
  Next
End Sub

--更新--

这是一个既能读又能写的修改版本:

Public Sub Lines()
  Dim oForm As FormFill

  Dim _
    aNextLines,
    aAllLines As String()

  Dim _
    i718Index,
    i720Index As Integer

  Dim _
    s718Line,
    s720Line As String

  oForm = New FormFill

  aAllLines = IO.File.ReadAllLines(oForm.FilePath)
  s718Line = String.Empty
  s720Line = String.Empty

  For i718Index = 0 To aAllLines.Length - 1
    If aAllLines(i718Index).StartsWith("718") Then
      s718Line = aAllLines(i718Index)
      aNextLines = aAllLines.Skip(i718Index + 1).ToArray

      For i720Index = 0 To aNextLines.Length - 1
        If aNextLines(i720Index).StartsWith("720") Then
          s720Line = aNextLines(i720Index)

          Exit For ' Assumes only one 720 line in the file
        End If
      Next

      Exit For ' Assumes only one 718 line in the file
    End If
  Next

  oForm.TextBox718.Text = s718Line
  oForm.TextBox720.Text = s720Line

  oForm.TextBox718.Tag = i718Index
  oForm.TextBox720.Tag = i720Index
End Sub

现在,在您的“保存”按钮的 Click 事件处理程序中:

Private Sub SaveButton_Click(Sender As Button, e As EventArgs) Handles SaveButton.Click
  Dim aAllLines As String()

  Dim _
    i718Index,
    i720Index As Integer

  Dim _
    s718Line,
    s720Line As String

  s718Line = Me.TextBox718.Text
  s720Line = Me.TextBox720.Text

  i718Index = Me.TextBox718.Tag
  i720Index = Me.TextBox720.Tag

  aAllLines = IO.File.ReadAllLines(Me.FilePath)

  aAllLines(i718Index) = s718Line
  aAllLines(i720Index) = s720Line

  IO.File.WriteAllLines(Me.FilePath, aAllLines)
End Sub

应该这样做。