Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
JelteDeJong
in reply to: Anonymous

you could try the following:

Dim splitChar As String = ","
Dim keyLine As String
Dim valLine As String

Using reader As New IO.StreamReader("c:\temp\test.csv", Text.Encoding.Default)
    keyLine = reader.ReadLine()
    valLine = reader.ReadLine()
End Using

Dim keys As String() = keyLine.Split(splitChar)
Dim vals As String() = valLine.Split(splitChar)

Dim csvData As Dictionary(Of String, String) = New Dictionary(Of String, String)()
For i = 0 To keys.Count - 1
    csvData.Add(keys(i), vals(i))
Next

For Each item As KeyValuePair(Of String, String) In csvData
    MsgBox(item.Key & ": " & item.Value)
Next

This will fail if the number of key and values are not the same. If you have control over how the csv file is made i would advice you to setup different. its better tou put a key/value pair on each line. like this:

length,1000

Width,500

Its more easy to read and the following iLogic rule will not fail enterly when a line is not correct.

Dim splitChar As String = ","
        Dim csvData As Dictionary(Of String, String) = New Dictionary(Of String, String)()

        Using reader As New IO.StreamReader("c:\temp\test.csv", Text.Encoding.Default)
            Dim line As String = reader.ReadLine

            Do While (Not line Is Nothing)
                Try
                    Dim splittedLine As String() = line.Split(splitChar)
                    csvData.Add(splittedLine(0), splittedLine(1))
                Catch ex As Exception
                    MsgBox("Could not add data from line: " & line)
                End Try
                line = reader.ReadLine
            Loop
        End Using

        For Each item As KeyValuePair(Of String, String) In csvData
            MsgBox(item.Key & ": " & item.Value)
        Next

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com