2015-04-02 68 views
0

我的代码是:VB.NET如何通过OpenFileDialog打开一个文本文件到Array和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 

它加载这样目前:

enter image description here

但我需要把它分成:

代理[0]和代理[1]
0是IP,1是端口。

我需要稍后在Web浏览器中使用它们。

我似乎无法找到一个方法来做到这一点,每当我试图

proxies.split(":") 

它给我一个错误:

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

回答

0

我只是做了今天这个你可以像使用替换功能。唯一的区别是我让它删除了一行前缀。不过,你也可以这样做后缀。试一试。

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")(You可能必须有单独的文本框来执行此第一个。)这分离的IP和端口,但离开港口。

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 
+0

在我看来这似乎相当复杂。 – 2015-04-02 19:51:42

+0

这看起来相当复杂,你可以使用较少的线条来实现,我只是不记得这一行。 – Khasym 2015-04-02 20:18:42

+0

是的,它来自我的一个程序。我故意使它变得复杂,所以反向工程将是一个痛苦。很抱歉它很复杂。 – Viper151 2015-04-02 22:30:35

1

proxies是列表框。请使用proxies.Items或者定义一个数组。

我觉得很难有一点了解你的问题,但这里是把IP地址和端口的列表框的例子:

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

输出:

项目1:1.160.129.53

项目2 :9064

+0

关于试用这个,看起来正是我要找的。 – Khasym 2015-04-02 20:19:49

0
  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 

我在找什么。

+0

这就是我写的,是不是:)?唯一的区别是,你分配了两个字符串项目1和2.(: – 2015-04-02 22:25:04

+0

但我很好奇你为什么要声明循环内的字符串? – 2015-04-02 22:30:33

+0

是的,但.Items也是添加,并感谢!超过1个代理,大约20,000 – Khasym 2015-04-03 04:36:00

相关问题