编辑由特定字符串找到的特定行,其中多次出现文本文件

时间:2015-10-06 03:31:14

标签: vb.net

我正在创建一个软件,该软件应该读取文本文件并仅显示某些字符串出现的某些行。我将附上我正在使用的文件:

[fltsim.0]
title=Boeing 737-800 Paint1
sim=Boeing737-800
model=
panel=
sound=
texture=1
kb_checklists=Boeing737-800_check
kb_reference=Boeing737-800_ref
atc_id=N737W
atc_airline=Boeing
atc_flight_number=
ui_manufacturer="Boeing"
ui_type="737-800"
ui_variation="Boeing livery"
ui_typerole="Commercial Airliner"
ui_createdby="Microsoft Corporation"
description="One should hardly be surprised that the world's most prolific manufacturer of commercial aircraft is also the producer of the world's most popular jetliner. The 737 became the best-selling commercial jetliner worldwide when orders for it hit 1,831 in June 1987 (surpassing Boeing's own 727 as the previous champ). However, it wasn't always that way\s in the first few years of production, there were so few orders that Boeing considered canceling the program. They didn't, and the airplane has more than proven itself in over three decades of service."

[fltsim.1]
title=Boeing 737-800 Paint2
sim=Boeing737-800
model=
panel=
sound=
texture=2
kb_checklists=Boeing737-800_check
kb_reference=Boeing737-800_ref
atc_id=N737X
atc_airline=World Travel
atc_flight_number=
ui_manufacturer="Boeing"
ui_type="737-800"
ui_variation="World Travel Airlines"
ui_typerole="Commercial Airliner"
ui_createdby="Microsoft Corporation"
description="One should hardly be surprised that the world's most prolific manufacturer of commercial aircraft is also the producer of the world's most popular jetliner. The 737 became the best-selling commercial jetliner worldwide when orders for it hit 1,831 in June 1987 (surpassing Boeing's own 727 as the previous champ). However, it wasn't always that way\s in the first few years of production, there were so few orders that Boeing considered canceling the program. They didn't, and the airplane has more than proven itself in over three decades of service."

正如您所看到的,标题,[fltsim.0,1等]每次递增1,每个标题下面都是我需要提取的大量信息。我希望拉出title={string}。我能够取出第一次出现的冠军,但我似乎无法弄清楚如何取出其余的。总而言之,我的目标是从该文件中读取特定数据(有多个出现),并以相同的方式写入文件。 示例:如果我想提取title={string},我想要获得波音737-800 Paint 1和波音737-800 Paint 2(它们可以被命名为任何东西)。 这是我正在使用的代码(仅发现第一次出现):

Dim path As String = "C:\Aircraft.cfg" '<<<<<CHANGE THIS LATER

        'GET LINE HUMBER OF STRING
        Dim firstNumber As Integer
        Dim toSearch = "ui_variation="
        Dim lineNumber = File.ReadLines(path).
                 Where(Function(l) l.Contains(toSearch))
                 Select(Function(l, index) index)    
        If lineNumber.Any Then
            firstNumber = lineNumber.First
        End If    
        'READS WHAT IS IN THE LINE WITH LINE NUMBER
        Using file As New StreamReader(path)
            For i As Integer = 1 To firstNumber + 1
                If file.ReadLine() Is Nothing Then
                    Throw New ArgumentOutOfRangeException("lineNumber")
                End If
            Next
            Dim line As String = file.ReadLine()
            If line Is Nothing Then
                Throw New ArgumentOutOfRangeException("lineNumber")
            End If
            MsgBox(line)
        End Using

1 个答案:

答案 0 :(得分:0)

您的文件看起来像一个ini文件,因此有some options用于读取该格式。但是,要获取特定键的所有值,您的代码几乎就在那里。您可以只选择值:

,而不是选择匹配行的索引并重新读取文件
Dim values As IEnumerable(Of String) =
    File.ReadLines(path).
        Where(Function(l) l.Contains(toSearch)).
        Select(Function(l) l.Split("="c)(1))
For Each value In values
    MsgBox(value)
Next

<强>更新

这是一种可能的(虽然不是特别有效)更新数据的方法:

' Note: ReadAllLines to read the complete file into memory
Dim lines = File.ReadAllLines(path)
For i = 0 To lines.Count - 1
    If lines(i).Contains(toSearch) Then
        lines(i) &= " - updated"
        ' or perhaps something like
        ' lines(i) = toSearch & "New Value"
    End If
Next
File.WriteAllLines(path, lines)