2013-10-18 109 views
0

我正在創建一個可以創建播放列表並從中播放音樂的程序。我旁邊有一個列表框和一個按鈕,並帶有下面的代碼。如何從.txt文件讀取特定行?

Dim MusicFiles() As String 
Public ListOfMusicFiles As New Dictionary(Of String, String) 

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then 
     MusicFiles = OpenFileDialog1.FileNames 
    End If 
    Try 
     For Each Item In MusicFiles 
      Dim ItemSplit() = Item.Split("\"c) 
      Dim ItemLast As String = ItemSplit(ItemSplit.Count - 1) 
      ItemLast = ItemLast.Remove(ItemLast.Count - 4, 4) 
      ListOfMusicFiles.Add(ItemLast, Item) 
     Next 

    Catch ex As Exception 

    End Try 
End Sub 

上面的代碼放置了所需音樂文件的名稱,但我需要以某種方式將它存儲在某處。我需要創建一個.txt文件,並可能在第一行中包含音樂文件的名稱,並在第二行中包含該音樂文件的名稱。所以,我需要閱讀兩行,並將其添加到

listofmusicfiles 

(即我創建的字典)

那麼,我可以將文件導入到列表框中。 任何幫助,將不勝感激。該應用還處於發展過程中,所以如果你有這樣做,一個完全不同的方式或更容易或更有效的方法,那將是巨大的:)

+3

所以你的問題是,如何從文件中讀取特定行(標題)或如何將詞典內容寫入一個文件(MSG文本) – Plutonix

+2

你看System.IO.File.ReadAllLines和System.IO.File.WriteAllLines? –

+1

你可能想要考慮使用更標準和靈活的東西,比如XML。 –

回答

0

回答到How to read specific lines from a .txt file?

#Region " Read TextFile Line " 

' [ Read TextFile Line Function ] 
' 
' // By Elektro [email protected] 
' 
' Examples : 
' 
' MsgBox(Read_TextFile_Line("C:\File.txt", 1)) 
' Dim str As String = Read_TextFile_Line("C:\File.txt", 3) 

Private Function Read_TextFile_Line(ByVal File As String, ByVal Line_Number As Long) As String 

    Dim Lines() As String = {String.Empty} 
    Dim Line_Length As Long = 0 

    Try 
     Lines = IO.File.ReadAllLines(File) 
     Line_Length = Lines.LongLength - 1 
     Return Lines(Line_Number - 1) 

    Catch ex As IO.FileNotFoundException 
     MessageBox.Show(String.Format("File not found: ""{0}""", _ 
             File), _ 
         Nothing, _ 
         MessageBoxButtons.OK, _ 
         MessageBoxIcon.Error) 

    Catch ex As IndexOutOfRangeException 
     MessageBox.Show(String.Format("Attempted to read line {0}, but ""{1}"" has {2} lines.", _ 
             Line_Number, _ 
             File, _ 
             Line_Length + 1), _ 
         Nothing, _ 
         MessageBoxButtons.OK, _ 
         MessageBoxIcon.Error) 

    Catch ex As Exception 
     Throw New Exception(String.Format("{0}: {1}", _ 
              ex.Message, _ 
              ex.StackTrace)) 

    Finally 
     Lines = Nothing 
     Line_Length = Nothing 

    End Try 

    Return Nothing 

End Function 

#End Region 
0

要回答你的最後一個問題,「所以如果你有一個完全不同的方式,或者更簡單或更有效的方式,那會很棒」,請使用數據集。

'You only need to define your data once 
    Dim ds As New DataSet 'I like saving datasets to file as opposed to datatables. Plus you get the advantage of have muliple tables if you need. 
    Dim dt As New DataTable 'The table will hold you data, you can have multiple tables for storing different types of data 
    'Add what ever columns you what to the table 
    dt.Columns.Add(New DataColumn("FileName", GetType(String))) 
    dt.Columns.Add(New DataColumn("FileLocation", GetType(String))) 
    'Add the table to the dataset 
    ds.Tables.Add(dt) 

    'Now you can add records to your table 
    Dim DR As DataRow 
    DR = dt.NewRow 'This builds a new record with the schema from your table, but you still have to fill in the data 
    'fill in the data columns you wanted 
    DR("FileName") = "..Your file name here.." 
    DR("FileLocation") = "..Your location here.." 
    'Now add the new row to your table 
    dt.Rows.Add(DR) 


    'Then, whenever you want, you can save to your HD like this: 
    ds.WriteXml("Your File Name", XmlWriteMode.WriteSchema) 'XmlWriteMode.WriteSchema IS IMPORTANT 

    'And can can read from the HD like this 
    ds.ReadXml("Your File Name") 
+0

但我不能單獨閱讀不同的行,對吧? 我想要分別顯示名稱和位置.. – Bread

+0

我不知道你的意思。它可以查詢或讀取或讀取特定的行或列或任何您想要的數據。您甚至可以輕鬆地對數據進行排序。這幾乎是你可以擁有的最靈活性。 – Steve

+0

我想做的是創建一個簡單的播放列表。我點擊播放列表中的其中一項,音樂播放。當我關閉應用程序並再次打開應用程序時,音樂文件應該仍然存在..任何想法? – Bread