2012-07-31 63 views
1

是否可以在循環內移動到下一行或臨時讀取下一行?我沒有找到任何有用的數據說明我會如何做到這一點,我的猜測是以某種方式找到您當前所在行的行號(索引),然後從您所在的位置讀取+1。移動到StreamReader中的下一行

Using TestFile As New IO.StreamReader(My.Settings.cfgPath & "tempRPT.txt", System.Text.Encoding.Default, False, 4096) 
     Do Until TestFile.EndOfStream 
      ScriptLine = TestFile.ReadLine 
      ScriptLine = LCase(ScriptLine) 
      If InStr(ScriptLine, "update: [b") Then 
       Dim m As Match = Regex.Match(ScriptLine, "\(([^)]*)\)") 
       builder.AppendLine(m.Value) 

       'This is where it would move to next line temporarily to read from it 
       If InStr(ScriptLine, "hive: write:") > 0 Or InStr(ScriptLine, "update: [b") > 0 Then 'And InStr(ScriptLine, "setmarkerposlocal.sqf") < 1 Then 
        builder.AppendLine(ScriptLine) 

       End If 
      End If 

     Loop 
    End Using 
+0

你可以用'System.IO.File.ReadAllLines()'在內存中讀取整個文件。 – ja72 2012-07-31 14:53:47

回答

3

試試這個。真正的所有線條,並把它們放在一個Queue(Of T)對象中。

Dim path As String = My.Settings.cfgPath & "tempRPT.txt" 

    Dim lines As String() = IO.File.ReadAllLines(path, System.Text.Encoding.Default) 
    Dim que = New Queue(Of String)(lines) 

    Do While que.Count > 0 
     ScriptLine = que.Dequeue() 
     ScriptLine = LCase(ScriptLine) 
     If InStr(ScriptLine, "update: [b") Then 
      Dim m As Match = Regex.Match(ScriptLine, "\(([^)]*)\)") 
      builder.AppendLine(m.Value) 

      Dim next_line As String = que.Peek  'Read next line temporarily    'This is where it would move to next line temporarily to read from it 
      If InStr(next_line, "hive: write:") > 0 Or InStr(next_line, "update: [b") > 0 Then 'And InStr(next_line, "setmarkerposlocal.sqf") < 1 Then 
       builder.AppendLine(next_line) 
      End If 
     End If 
    Loop 
+0

確保你不要在空集合中偷看。 – ja72 2012-07-31 15:04:32

+0

這個和NoAlias的答案都很棒。我要先試試這個,看看它是如何工作的。我很感謝:) – 2012-07-31 15:23:23

+0

最終我最終使用你的。謝謝你的回答! – 2012-07-31 15:51:17

1

假設您沒有使用.Net的舊版本,您可能會發現使用字符串列表更容易。

Dim lstLinesInTextFile As List(of String) = IO.File.ReadAllLines(My.Settings.cfgPath & "tempRPT.txt").ToList() 

     Dim intCursor As Integer = 0 
     For Each strLine As String In lstLinesInTextFile 

      'Perform Logic Here 

      'To View Next Line: 
      If lstLinesInTextFile.Count > intCursor + 1 Then 

       Dim strNextLine As String = lstLinesInTextFile(intCursor + 1) 

       'Perform Logic Here. 

      End If 

      intCursor += 1 

     Next