简体   繁体   English

将.txt文件在线列表读入列表框VB.NET

[英]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. 我试图从在线链接中读取列表框,例如: http//blahblah.com/list.txt到列表框中。 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.. 然后将其保存为.txt文件,然后将其读入行列表框行。

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 . 将文件内容读入String变量,使用String.Split创建包含各行的数组,然后将该数组中的项添加到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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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