c# - 如何访问特定线路
问题描述
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,
在一行中。
解决方案
如果你想要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);
}
推荐阅读
- c# - 编码的 UI 测试生成器 - “未记录最后一次鼠标操作”并且无法记录该操作
- unity3d-editor - 统一隐藏对象而不改变它们的状态
- php - 在php/mysql中生成临时登录
- ios - 如何在 iOS swift 项目中删除部分 pod 文件?
- sql - 从 Postgres JSONB 字段中选择缺失值
- c# - 多次运行函数时无法访问已处置的对象
- python - python eve - 删除上的 data_relation - 如果引用则保留/错误
- file - 如何从flutter中的存储中获取所有特定类型的文件
- matlab - 使用 textread 读取带有文本的 CSV 文件
- git - webhook 是否触发源/开发或开发分支的管道