好的,所以我有一个带有示例内容的文本文件
line1with some random stuff here
line 2 with something very completely different
line3 has some other neat stuff
line4 is the last line in the textfile
Run Code Online (Sandbox Code Playgroud)
我需要获取该文本文件,删除一行,并保留文件的REST.所以如果我想删除第2行,id想要一个这样的结果:
line1with some random stuff here
line3 has some other neat stuff
line4 is the last line in the textfile
Run Code Online (Sandbox Code Playgroud)
请注意,我不希望线之间留下任何空间.
〜代码示例请!即使这是不可能的,任何帮助将不胜感激!:)
最简单的方法是这样的:
string filePath = ...
List<string> lines = new List<string>(File.ReadAllLines(filePath ));
lines.RemoveAt(2); //Remove the third line; the index is 0 based
File.WriteAllLines(filePath, lines.ToArray());
Run Code Online (Sandbox Code Playgroud)
请注意,对于大型文件,这将是非常低效的.