首页 文章

将文件从文件主机加载到列表框中

提问于
浏览
0

我正在制作一个程序来阻止使用文件主机的网站,我使用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结合使用 . 或者有更好的方法来做到这一点?
谢谢 .

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;
    

相关问题