我正在尝试制作一个存档器,其中高达 1 Gb 或更大的文件很常见,而 File 类并不是真的有用。有什么方法可以更有效地使用内存来成功处理更大的文件?这是我为此目的最有效的代码。
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; }
}