我正在创建一个软件,该软件应该读取文本文件并仅显示出现特定字符串的特定行。我将附加我正在使用的文件:
[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,etc。]每次递增1,并且在每个标头的下面是我需要提取的大量信息。我想退出title={string}
。我能够拿出第一次出现的标题,但是我似乎无法弄清楚如何拿出其余的标题。总而言之,我的目标是从该文件中读取特定数据(发生多次),并以相同的方式写入该文件。示例:如果我想退出title={string}
,我想购买Boeing 737-800 Paint 1和Boeing 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
您的文件看起来像是ini文件,因此有一些用于读取该格式的选项。但是,要获取特定键的所有值,您的代码几乎就在那里。您可以选择以下值来代替选择匹配行的索引并重新读取文件:
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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句