简体   繁体   English

C#-将文本文件内容修剪为指定的行号

[英]C# - trim text file contents to specified line number

I have a very annoying bug, code generation tool has generated like 20,000 lines of rubbish in file. 我有一个非常烦人的错误,代码生成工具已在文件中生成了20,000行垃圾。 Removing it all by hand, is, well, rather hard, so I want to write a program that does so. 用手将其全部删除非常困难,所以我想编写一个程序来删除它。 Good news is that source code file contains needful info on lines 1 - 300, and then rubbish all the way down to line 20,000. 好消息是,源代码文件在第1-300行包含必要的信息,然后一直到20,000行都包含垃圾。

I'm not very experienced in handling files in C#, and couldn't google up method I need. 我在C#中处理文件的经验不是很丰富,也无法用谷歌搜索所需的方法。 Are there any ways to do so? 有什么办法吗?

File.WriteAllLines("test.txt", File.ReadAllLines("test.txt").Take(300));

Below is an example code to do this file trimming: 以下是执行此文件修剪的示例代码:

public static void TrimFile(string fileName,int start, int end)
        {
            File.WriteAllLines(fileName,
                File.ReadAllLines(fileName)
                    .Skip(start - 1).Take(end - start));

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM