首页 > 解决方案 > 如何访问特定线路

问题描述

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, 

在一行中。

标签: c#file

解决方案


如果你想要Skip第一7行,你可以在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);
   }

推荐阅读