0

我正在制作一个程序来阻止使用文件主机的网站,我使用流式阅读器来读取它,并且在加载表单时将它的一些行加载到列表框中时遇到了一些问题。
因此,这是示例:
文件主机:

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

当程序启动时,列表框看起来像这样

第一网 第二网 第
三网

因此,程序仅在列表框中显示以“127.0.0.1”开头并以“#onlythis”结尾的地址。我做了一些浏览,也许这可以通过使用 StartsWith、Endswith 或 Contain 来实现。但我不知道如何使用它,我不知道它是否可以与streamreader结合使用。或者也许有更好的方法来做到这一点?
谢谢。

4

1 回答 1

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;
于 2013-06-01T17:27:37.947 回答