Semikilon getrennte Datei lesen und an zwei Variablen übergeben

  • VB.NET

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von mt14516.

    Semikilon getrennte Datei lesen und an zwei Variablen übergeben

    Hallo Zsuammen,

    steh grade total aufm Schlauch. Habe ne Textdatei die zwei Werte enthält durch Semikolon getrennt. Ich muss diese in 2 Variablen einlesen. Preis und Artikelname.

    VB.NET-Quellcode

    1. Using MyReader As New Microsoft.VisualBasic.
    2. FileIO.TextFieldParser(
    3. "C:\TestFolder\test.txt")
    4. MyReader.TextFieldType = FileIO.FieldType.Delimited
    5. MyReader.SetDelimiters(";")
    6. Dim currentRow As String()
    7. While Not MyReader.EndOfData
    8. Try
    9. currentRow = MyReader.ReadFields()
    10. Dim currentField As String
    11. For Each currentField In currentRow
    12. MsgBox(currentField)
    13. Next
    14. Catch ex As Microsoft.VisualBasic.
    15. FileIO.MalformedLineException
    16. MsgBox("Line " & ex.Message &
    17. "is not valid and will be skipped.")
    18. End Try
    19. End While


    so hab ich nur die ANzeige in der MessageBox. Ich steh wie gesagt aufm Schlauch. Kann mir jemand helfen bitte?


    Gruß
    Michael
    @mt14516 Probierma

    VB.NET-Quellcode

    1. Dim parts = currentRow.Split(";"c)
    2. MessageBox.Show(String.Format("{0} => {1}", parts(0), parts(1)))
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Dann aber so:

    VB.NET-Quellcode

    1. Using MyReader As New Microsoft.VisualBasic.
    2. FileIO.TextFieldParser(
    3. "C:\TestFolder\test.txt")
    4. MyReader.TextFieldType = FileIO.FieldType.Delimited
    5. MyReader.SetDelimiters(";")
    6. 'Dim currentRow As String()
    7. While Not MyReader.EndOfData
    8. Try
    9. Dim currentRow As String = MyReader.ReadLine
    10. 'currentRow = MyReader.ReadFields()
    11. 'Dim currentField As String
    12. 'For Each currentField In currentRow
    13. ' MsgBox(currentField)
    14. 'Next
    15. Dim parts = currentRow.Split(";"c)
    16. MessageBox.Show(String.Format("{0} => {1}", parts(0), parts(1)))
    17. Catch ex As Microsoft.VisualBasic.
    18. FileIO.MalformedLineException
    19. MsgBox("Line " & ex.Message &
    20. "is not valid and will be skipped.")
    21. End Try
    22. End While
    23. End Using