[英]VB.NET How to open a text file via OpenFileDialog into Array and the ListBox?
我的代码是:
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
当前加载如下:
但我需要将其分为:
代理[0]和代理[1]
0是IP,1是端口。
稍后,我需要在Web浏览器中使用它们。
每当我尝试时,我似乎都找不到办法
proxies.split(":")
它给我一个错误:
.split isn't a member of system.windows.forms.listbox
proxies
是ListBox。 使用proxies.Items
或定义一个数组。
我很难理解您的问题,但这是将IP和端口放入ListBox的示例:
For Each address As String In File.ReadAllLines(dialog.FileName)
proxies.Items.AddRange(address.Split(":"))
Next
输出:
项目1: 1.160.129.53
第2项: 9064
等等
今天我只是做了这样的事情,您可以使用replace函数。 唯一的区别是,我删除了该行的前缀。 但是,您也可以添加后缀。 试试看。
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
对于您的情况:(只需执行列表“ 1.106.129.53:9064”中的第一个)(您可能必须具有单独的文本框才能执行此操作。)这将ip和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
是我一直在寻找的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.