简体   繁体   中英

VB.NET How to open a text file via OpenFileDialog into Array and the ListBox?

My code is:

    Using dialog As OpenFileDialog = New OpenFileDialog
            dialog.Filter = "Text Files (*.TXT;)|*.TXT|All files (*.*)|*.*"
            If (dialog.ShowDialog = DialogResult.OK) Then
            proxies.Items.AddRange(File.ReadAllLines(dialog.FileName))
            log.Text &= Environment.NewLine & "Proxies Loaded"
        End If
    End Using

It loads like this currently:

在此处输入图片说明

But I need to divide it into:

proxies[0] and a proxies[1]
0 being the IP and 1 being the port.

I need to use these in a Web Browser, later.

I can't seem to find a way to do this, whenever I try

proxies.split(":")

It give me an error:

.split isn't a member of system.windows.forms.listbox

proxies is the ListBox. Either use proxies.Items or define an array instead.

I find it a little hard understanding your question but here's an example of putting the IPs and ports in the ListBox:

For Each address As String In File.ReadAllLines(dialog.FileName)
    proxies.Items.AddRange(address.Split(":"))
Next

Output:

Item 1: 1.160.129.53

Item 2: 9064

etc.

I just did something like this today you can use the replace function. The only difference is that I made it remove a prefix to the line. However you can do this to suffixes aswell. Give it a try.

Dim lines() As String = TextBox1.Lines
Dim textreplace As String = "TYPE THE STUFF YOU WANT CUT OFF IN HERE"
Dim texttoreplace As String = ""
Dim textline As String = lines(30)'The Line # you have it on. Remember the line 1 is line 0
textline = textline.Replace(textreplace, texttoreplace)
TextBox2.Text = textline 'Here the text is sent in

FOR YOUR CASE: (Just doing the first one in your list "1.106.129.53:9064")(You may have to have separate textboxes to perform this.)This separates the ip and port but leaves the port.

Dim lines() As String = seperate_textbox_or_you_can_use_another_way_to_load_it.Lines
Dim textreplace As String = "1.106.129.53:"
Dim texttoreplace As String = ""
Dim textline As String = lines(1)'The Line # you have it on. Remember the line 1 is line 0
textline = textline.Replace(textreplace, texttoreplace)
log.Text = textline 'Here the text is sent in
            For Each address As String In File.ReadAllLines(dialog.FileName)
                proxies.Items.AddRange(address.Split(":"))
                Dim ip As String = proxies.Items(0)
                Dim port As String = proxies.Items(1)
            Next

Is what I was looking for.

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