如何访问特定线路

时间:2019-12-02 13:57:52

标签: c# file

private void Button4_Click(object sender, EventArgs e) 
        {
            //Finding your file and assigning it as a string.
            string start = Directory.GetCurrentDirectory() + @"\file.txt";

            using (var streamReader = new StreamReader(start))
            {
                string line = streamReader.ReadLine();

                int[] values = line.Split(' ').Select(int.Parse).ToArray();

                Array.Sort(values);

                Array.Reverse(values);

                for (int i = 0; i < values.Length; i++)
                {
                    richTextBox4.AppendText(values[i] + " ");
                }
            }
        }

所以我需要访问8-9-10行,我的.txt文件是: https://gyazo.com/7ac43e9c5a4cb4d17393e429657778ae

8-9-10行也必须是这样的:

28 80 62 30 68 77 71 64 54 84 57 37, 

一行。

1 个答案:

答案 0 :(得分:3)

如果您想Skip7行,可以在 Linq 的帮助下完成:

   using System.IO;
   using System.Linq;

   ...

   private void Button4_Click(object sender, EventArgs e) {
     var numbers = File
       .ReadLines(Path.Combine(Directory.GetCurrentDirectory(), "file.txt"))
       .Skip(7)
    // .Take(3) // Uncomment it if you want to take at most 3 lines after skip
       .SelectMany(line => line.Split(' ')); 

     richTextBox4.Text = string.Join(" ", numbers);
   }