开发者

C# - trim text file contents to specified line number

开发者 https://www.devze.com 2023-04-07 13:21 出处:网络
I have a very annoying bug, code generation tool has generated like 20,000 lines of rubbish in file. Removing it all by hand, is, well, rather hard, so I want to write a program that does so. Good new

I have a very annoying bug, code generation tool has generated like 20,000 lines of rubbish in file. 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.

I'm not very experienced in开发者_如何学Python handling files in C#, and couldn't google up method I need. 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));

        }
0

精彩评论

暂无评论...
验证码 换一张
取 消