streamreader没有得到适当的行数

时间:2018-04-11 02:58:23

标签: c# file line readfile streamreader

它有23个,但有30行是&#34;占位符:占位符&#34; ........................... ................................................. < / p>

      using (StreamReader Reader = new StreamReader(File.Open(AccountsFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
            {
                    string line;
                    while ((line = Reader.ReadLine()) != null)
                    {
                        if (line == "Placeholder:Placeholder")
                        {
                            int count = line.Count();  
                        }
                    }
            }

1 个答案:

答案 0 :(得分:0)

声明一个变量来存储计数,并在每次一行与模式字符串匹配时递增它。

using (StreamReader Reader = new StreamReader(File.Open(AccountsFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
{
    string line;
    int count = 0;
    while ((line = Reader.ReadLine()) != null)
    {
        if (line == "Placeholder:Placeholder")
        {
            count++;
        }
    }
}