2015-04-02 66 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

相關問題