0

I'm trying to make an archiver, where files up to 1 Gb or more are preety common and File class is not really useful. Is there any way to use memory more efficiently to handle bigger files successfully? Here is my most efficient code for this purpouse.

int CountLines(string path)
{
    try
    {
        int counter = 0;
        string line;
        System.IO.StreamReader file = new System.IO.StreamReader(resPath);
        while ((line = file.ReadLine()) != null) counter++;
        file.Close();
        return counter;
    }
    catch { return -1; }
}
  • 3
    Does this answer your question? [Is there a better way to determine the number of lines in a large txt file(1-2 GB)?](https://stackoverflow.com/questions/36367368/) and [Counting the # of lines in a very large file gives System OutofMemory Exception](https://stackoverflow.com/questions/45194927/) and [What's the fastest way to count the total lines of text file in c#?](https://stackoverflow.com/questions/19794781/) and [Determine the number of lines within a text file](https://stackoverflow.com/questions/119559/) –  Aug 21 '21 at 13:14
  • 1
    [Counting Lines of a Text File in C#, the Smart Way](https://www.nimaara.com/counting-lines-of-a-text-file/) • [How to get line count in a file without reading](https://stackoverflow.com/questions/13914786/) • [What is a quick way to count lines in a 4TB file? => make a rough approximation on a section/slice/extract!](https://unix.stackexchange.com/questions/504892/) –  Aug 21 '21 at 13:19

0 Answers0