C#列表框拆分为另外2个列表框
本文关键字:列表 2个 拆分 | 更新日期: 2023-09-27 18:06:31
问题:我有1个ListBox,它加载一个包含以下内容的文本文件:
- ip:端口
- ip:端口
- ip:端口
我想做的是,一旦我将文本文件加载到列表框中,我就想让"ip"进入另一个列表框,并让"port"进入不同的列表框。这是第一次做这样的项目。
// 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]);
});
类似的东西
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] );
}
}