This question already has answers here:
How do I read a specified line in a text file?
                                
                                    (14个回答)
                                
                        
                                3年前关闭。
            
                    
我想从文本文件中读取第4、5和6行,并将它们存储为单独的字符串。这是我的代码,它会打印出文本文件中的每一行。

ReadFromFile(@"C:\Users\Eoghan\Documents\Assign02_Data.txt");


 static void ReadFromFile(string aTextFile)
    {
        System.IO.StreamReader InputFile = new System.IO.StreamReader(aTextFile);
        string s;
        while (!InputFile.EndOfStream)
        {
            s = InputFile.ReadLine();
            Console.WriteLine(s);
        }
    }

最佳答案

List<string> lines = File.ReadLines(filename).Skip(3).Take(3).ToList();

09-06 22:35