2013-10-18 3 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 

(내가 만든 사전)에 추가

후, 나는 목록 상자로 파일을 가져올 수 있습니다해야합니다. 도움이 될 것입니다. 이 응용 프로그램은 당신이를, 일을 완전히 다른 방식으로 또는 쉽게 또는 더 효율적인 방법이 있다면, 그래서 그 것이다, 개발 과정에서 여전히 How to read specific lines from a .txt file?에 응답 큰 :)

+3

그래서 귀하의 질문에 – Plutonix

+2

당신이 System.IO.File.ReadAllLines 봤어 파일에서 특정 라인을 어떻게 파일 (MSG 텍스트)에 사전 내용을 작성하는 방법 (제목) 또는 읽기 및 방법, 무엇을 System.IO.File.WriteAllLines? –

+1

XML과 같이 좀 더 표준적이고 유연한 것을 사용하는 것이 좋습니다. –

답변

0

:

#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

관련 문제