利用 VB.NET / C# 读取TXT文件中的第一行或者最后一行。第一行很简单,直接用 Readline 就能读出来了。如果想显示最后一行,一般是需要计算出文本总行数,然后以数组形式保存,最后直接调用数组最后一组数据就是最后一行的文本。当然,Forece 也发现了不少其他方法。

VB 代码

方法1:

1
2
3
4
5
6
7
8
9
10
11
12
13
        Dim sLine As String = ""
        Dim arrText As New ArrayList
        Dim objReader As New StreamReader("c:\temp\test.txt")
        Dim s As String
        Do
            sLine = objReader.ReadLine()
            If Not sLine Is Nothing Then
                arrText.Add(sLine)
            End If
        Loop Until sLine Is Nothing
        s = arrText.Count 's就是文本总行数
        RichTextBox1.Text = arrText(0)
        RichTextBox2.Text = arrText(s - 1) ' 因为数组是从0开始数的,所以最后一行是 s - 1

方法2:

1
2
3
4
5
        Dim str As New StreamReader("c:\temp\test.txt")
        RichTextBox1.Text = str.ReadLine()
        While (Not str.EndOfStream)
            RichTextBox2.Text = str.ReadLine()
        End While

方法3:

1
2
3
        Dim lines() As String = IO.File.ReadAllLines("c:\temp\test.txt")
        RichTextBox1.Text = lines(0)
        RichTextBox2.Text = lines(lines.Length - 1)

方法4:

1
2
3
        Dim lines As String() = File.ReadAllLines("c:\temp\test.txt")
        RichTextBox1.Text = lines.First
        RichTextBox2.Text = lines.Last

方法5:

1
2
3
        Dim lines = IO.File.ReadLines("c:\temp\test.txt")
        RichTextBox1.Text = lines.FirstOrDefault
        RichTextBox2.Text = lines.LastOrDefault

方法6:

1
2
3
4
        Dim lines As New StreamReader("c:\temp\test.txt")
        Dim s = lines.ReadToEnd().Split(vbLf).Length
        RichTextBox1.Text = File.ReadLines("c:\temp\test.txt").Skip(0).Take(1).First()
        RichTextBox2.Text = File.ReadLines("c:\temp\test.txt").Skip(s - 1).Take(1).First()

C# 代码

方法1:
需要引用 using System.Collections;

1
2
3
4
5
6
7
8
9
10
11
            StreamReader sr = new StreamReader(@"c:\temp\test.txt");
            String sLine;
            ArrayList arrText = new ArrayList();
            int i = 0;
            while ((sLine = sr.ReadLine()) != null)
            {
                arrText.Add(sLine);
                i++;
            }
            richTextBox1.Text = arrText[0].ToString();
            richTextBox2.Text = arrText[i-1].ToString();

方法2:

1
2
3
4
5
6
            StreamReader str = new StreamReader(@"C:\temp\test.txt");
            richTextBox1.Text = str.ReadLine();
            while (!str.EndOfStream)
            {
                richTextBox2.Text = str.ReadLine();
            }

方法3:

1
2
3
            string[] lines = System.IO.File.ReadAllLines(@"c:\temp\test.txt");
            richTextBox1.Text = lines[0];
            richTextBox2.Text = lines[lines.Length - 1];

方法4:

1
2
3
            string[] lines = System.IO.File.ReadAllLines(@"c:\temp\test.txt");
            richTextBox1.Text = lines.First();
            richTextBox2.Text = lines.Last();

方法5:

1
2
3
            var lines = File.ReadLines(@"c:\temp\test.txt");
            richTextBox1.Text = lines.First();
            richTextBox2.Text = lines.Last();

方法6:

1
2
3
4
            StreamReader lines = new StreamReader(@"c:\temp\test.txt");
            int s = lines.ReadToEnd().Split('\n').Length;
            richTextBox1.Text = File.ReadLines(@"c:\temp\test.txt").Skip(0).Take(1).First();
            richTextBox2.Text = File.ReadLines(@"c:\temp\test.txt").Skip(s-1).Take(1).First();