我正在制作一个程序来阻止使用文件主机的网站,我使用了streamreader来读取它,我在加载表单时将其部分行加载到列表框中有一些问题。
所以,这是一个例子:
文件主持人:
127.0.0.1 first.com #onlythis
127.0.0.1 second.com #onlythis
127.0.0.1 third.com #onlythis
127.0.0.1 fourth.com
255.255.255.255 fifth.com
255.255.255.255 sixth.com #onlythis
当程序启动时,列表框将如下所示
first.com
second.com
third.com
因此,程序只显示列表框中以“127.0.0.1”开头并以“#onlythis”结尾的地址。我已经做了一些浏览,也许这可以通过使用StartsWith,Endswith或者Contain来实现。但我不知道如何使用它,我不知道它是否可以与streamreader结合使用。或者有更好的方法来做到这一点? 感谢。
答案 0 :(得分:0)
假设他们都将采用这种特定格式:
string Path = @"C:\test.txt";
List<string> BlockedHosts = new List<string>();
using (StreamReader read = new StreamReader(Path))
{
while (!read.EndOfStream)
{
string[] data = read.ReadLine().Split(' ');
if (data.Count() >= 3)
{
if (data[0] == "127.0.0.1" && data[2] == "#onlythis")
{
BlockedHosts.Add(data[1]);
}
}
}
}
//Setting data in listbox
listBox1.DataSource = BlockedHosts;