我正在创建一个可以创建播放列表并播放音乐的程序。我有一个列表框和一个按钮旁边,并带有以下代码。
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
(我创建的字典)
然后,我可以将文件导入列表框。 任何帮助,将不胜感激。应用程序仍处于开发过程中,因此如果您有一种不同的方式,或者更简单或更有效的方式,那将是很好的:)
答案 0 :(得分: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
答案 1 :(得分: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")