如何从vb.net中的文本文件中获取行号

时间:2016-09-10 01:33:07

标签: vb.net

我正在制作一个程序,其中我有2个文本文件,我希望从每个文本文件中获取2行,如下所示

onCreate()

我很感激帮助(请尝试2简单对不起,但我不是那么好THANX)

2 个答案:

答案 0 :(得分:0)

您可以实例化几个Integer类型的变量来帮助解决这个问题。当您遍历列表时,一个存储行号,另一个存储您正在寻找的行号。此外,通过将For Each方法更改为Do Until或Do While方法,如果在文件的最后一行之前找到目标,则可能会加快速度。

Dim intLineNumber As Integer = -1
Dim intLineCursor As Integer

Do Until (intLineCursor = allLines.Count OrElse intLineNumber <> -1)

    If pathlocal & "from.txt".Contains(My.Computer.FileSystem.GetFileInfo(allLines(intLineCursor)).Name) Then

        intLineNumber = intLineCursor '+ 1 if you are displaying to a user since the lower bound is 0 based.

    End If      

    'Increment the cursor variable.
    intLineCursor += 1

Loop

答案 1 :(得分:0)

这可能会对您有所帮助:

Imports System.IO
Imports System
Imports System.Collections.Generic
Public Class Form1
Dim path As String = "Your Path"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim indxto As Integer = 0
    Dim indxfrom As Integer = 0
    Dim allLinesto As List(Of String) = File.ReadAllLines(path & "\" & "to.txt").ToList
    Dim allLinesfrom As List(Of String) = File.ReadAllLines(path & "\" & "from.txt").ToList
    For Each line As String In allLinesto
        indxto = allLinesto.IndexOf(line)
        'Debug.Print(line & " " & indxto.ToString)
        For Each item As String In allLinesfrom
            indxfrom = allLinesto.IndexOf(item)
            If line = item Then
                Debug.Print(item & " " & indxfrom.ToString)
            End If
        Next
    Next
End Sub

结束班