0

我正在创建一个可以创建播放列表并从中播放音乐的程序。我有一个列表框和一个按钮,旁边有以下代码。

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

(我创建的字典)

然后,我可以将文件导入列表框。任何帮助,将不胜感激。该应用程序仍处于开发过程中,因此,如果您有完全不同的方法,或者更简单或更有效的方法,那就太好了:)

4

2 回答 2

0

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

#Region " Read TextFile Line "

' [ Read TextFile Line Function ]
'
' // By Elektro H@cker
'
' 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
于 2013-10-18T18:27:32.977 回答
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")
于 2013-10-18T19:21:54.550 回答