1
问题: 我已经它加载的文本文件1个列表框包含:C#列表框被划分到另一2个列表框
- IP:端口
- IP:端口
- IP:端口
我想要做的是,一旦我将文本文件加载到列表框中,我想让'ip'进入不同的列表框,将'port'放入不同的列表框中。这是第一次处理这样的项目。
问题: 我已经它加载的文本文件1个列表框包含:C#列表框被划分到另一2个列表框
我想要做的是,一旦我将文本文件加载到列表框中,我想让'ip'进入不同的列表框,将'port'放入不同的列表框中。这是第一次处理这样的项目。
喜欢的东西
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]);
}
}
// 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]);
});
分割(..)方法接受字符作为BTW参数。字符串生成编译器错误:) –
谢谢,这工作完美。 – user911072