2015-10-06 39 views
0

我正在創建一個應該讀取文本文件並僅顯示特定字符​​串出現的特定行的軟件。我會附上我使用的文件:[fltsim.0,1等]由一個每次編輯由多個文本文件出現的特定字符串找到的特定行

[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." 

正如你所看到的,頭,增量,每頭下方是一個信息的主機我需要退出。我想抽出title={string}。我能夠抽出第一個冠軍頭銜,但我似乎無法弄清楚如何將剩下的東西拿出來。總而言之,我的目標是從該文件中讀取特定數據(不止一次),並以相同的方式寫入文件。例如:如果我想抽出title={string},我想要得到波音737-800油漆1和波音737-800油漆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 

回答

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 

UPDATE

這裏的更新數據的一個可能的(雖然不是特別有效)的方式:

' 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) 
+0

好吧,這工作完美!非常感謝。我是否可以使用相同的格式以相同的方式寫入文件(即在'title ='中更改值) – Sid

+0

@Sid LINQ不是更新數據的好選擇。使用我鏈接到的INI文件API可以更好地工作(儘管您必須循環檢查存在多少節)。您也可以只讀取所有數據,循環遍歷它以進行更新,然後再次寫出文件(假設文件適合內存)。我會添加一個例子。對於大文件,可能是[這個答案](http://stackoverflow.com/a/6394534/2278086)中的最後一個選項會更有用。 – Mark

相關問題