I am trying to parse each line of a text file but it keeps giving me an error ‘System.IndexOutOfRangeException’.
Each line in the ‘servers.lst’ is sequenced as:
Server Name|Server Host|Server Port|Server URL|Client Type|Client Version|Client URL
Each line represent one server. But I am unable to parse it correctly. It either returns the full file, blank variable, or error.
Here is my code:
Dim reader As String = My.Computer.FileSystem.ReadAllText("servers.lst")
Dim s() As String
Dim TotalServers As String = reader.Length
Dim x As Integer = 0
Dim myArray As String() = reader.Split("|"c)
For x = 1 To TotalServers
Servers(x).ServerName = myArray(0)
Servers(x).ServerIP = myArray(1)
Servers(x).ServerPort = myArray(2)
Servers(x).ServerURL = myArray(3)
Servers(x).Client = myArray(4)
Servers(x).ClientVer = myArray(5)
Servers(x).ClientURL = myArray(6)
Form1.ListBox1.Items.Add(Servers(x).ServerName)
x += 1
Next
I am trying to return each server’s name but I am having difficulties.
*Edit: Here is my structure code:
Structure ServerData
Public ServerName As String
Public ServerPort As Long
Public ServerURL As String
Public ServerIP As String
Public Client As String
Public ClientVer As String
Public ClientURL As String
End Structure
Public Servers As ServerData()
Thank You.
This line of code doesn’t looks right
First of all you should split your file based on the new line. Then only, from the newly created array based on every new line, you create another array, split it with the ‘|’ sign.
Here’s my shot. This code is not tested. Just write it on the fly but I guess you should get the idea.