처럼 Skip()
및 Take()
을 사용할 수 있습니다 당신이 전체 파일을 어느쪽으로 든 읽어야하기 때문에 Queue<string>
의 조합으로.
string line1 = meQueue.Dequeue();
string line2 = meQueue.Dequeue(); // <-- this is the last line.
또는 RichTextBox
에 이것을 추가 :
// if you want to read more lines change this to the ammount of lines you want
const int LINES_KEPT = 2;
Queue<string> meQueue = new Queue<string>();
using (StreamReader reader = new StreamReader(File.OpenRead("C:\\test.log")))
{
string line = string.Empty;
while ((line = reader.ReadLine()) != null)
{
if (meQueue.Count == LINES_KEPT )
meQueue.Dequeue();
meQueue.Enqueue(line);
}
}
지금 당신은 바로 그런 것처럼이 두 라인을 사용할 수 있습니다 File.ReadAllLines
를 사용
richTextBox1.Text = string.Empty; // clear the text
while (meQueue.Count != 0)
{
richTextBox1.Text += meQueue.Dequeue(); // add all lines in the same order as they were in file
}
사용 후 전체 텍스트를 읽을 것이다 Linq
은 이미 빨간색 선을 반복합니다. 이 메소드는 모든 것을 한 번에 실행합니다.
이 작업을 완벽하게 수행해 주셔서 감사합니다. –