Search code examples
c#filelinereadfilestreamreader

streamreader not getting proper count of lines


It counts 23 but there are 30 lines that are "Placeholder:Placeholder"............................................................................

      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();  
                        }
                    }
            }

Solution

  • Declare a variable to store the count and increment it every time a line matches a pattern string.

    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++;
            }
        }
    }