简体   繁体   中英

Read list from .txt file online into listbox VB.NET

I am trying to read a listbox from an online link for example: http://blahblah.com/list.txt into a listbox. each line into a new listbox item.

I could get around that by doing

dim wc as new webclient
textbox.text = wc.downloadstring("http://blahblah.com/list.txt")

then save it as a .txt file and then read it into a listbox line for line..

I'd like to skip that step and just read directly from the text file online..

Thank you and I will answer any questions you need for helping me with this code. Thanks a bunch.

You can't read the individual lines directly from the file without downloading it first. It's only available as a text file; you have to retrieve the contents of that file before you can access them.

You can do it without saving it into an actual text file, however. Read the file content into a String variable, use String.Split to create an array containing the individual lines, and then add the items from that array into your ListBox .

Dim Lines() As String
Dim stringSeparators() As String = {vbCrLf}
Dim Source As String
Dim wc as new WebClient

Source = wc.downloadstring("http://blahblah.com/list.txt");
Lines = Source.Split(stringSeparators, StringSplitOptions.None)
ForEach s As String in Lines
    ListBox1.Items.Add(s)
Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM