2011-08-25 71 views
1

問題: 我已經它加載的文本文件1個列表框包含:C#列表框被劃分到另一2個列表框

  • IP:端口
  • IP:端口
  • IP:端口

我想要做的是,一旦我將文本文件加載到列表框中,我想讓'ip'進入不同的列表框,將'port'放入不同的列表框中。這是第一次處理這樣的項目。

回答

0

喜歡的東西

 using (StreamReader sr = new StreamReader("TestFile.txt")) 
     { 
      String line; 
      // Read and display lines from the file until the end of 
      // the file is reached. 
      while ((line = sr.ReadLine()) != null) 
      { 
       string[] ipandport = line.split(":"); 
       lstBoxIp.Items.Add(ipandport[0]); 
       lstBoxPort.Items.Add(ipandport[1]); 
      } 
     } 
+1

分割(..)方法接受字符作爲BTW參數。字符串生成編譯器錯誤:) –

+0

謝謝,這工作完美。 – user911072

1
// if you wanted to do it with LINQ. 
// of course you're loading all lines 
// into memory at once here of which 
// you'd have to do regardless 
var text = File.ReadAllLines("TestFile.txt"); 
var ipsAndPorts = text.Select(l => l.Split(':')).ToList(); 

ipsAndPorts.ForEach(ipAndPort => 
{ 
    lstBoxIp.Items.Add(ipAndPort[0]); 
    lstBoxPort.Items.Add(ipAndPort[1]); 
}); 
相關問題