[英]StreamReader read file and StreamWriter write that result omitting first line?
Using the following code: 使用以下代码:
string lines = "";
using (StreamReader sr = new StreamReader(@"file.txt"))
{
lines = sr.ReadLine();
}
using (StreamWriter writer = new StreamWriter(@"file.txt"))
{
writer.Write(lines); // Change this to skip the first line
}
How can I make it rewrite everything EXCEPT the first line? 如何使它重写第一行以外的所有内容?
var allLinesExceptFirstOne = System.IO.File.ReadAllLines(filename).Skip(1).ToList();
Maybe you can try this: 也许您可以尝试以下方法:
var lines = File.ReadLines("file.txt").Skip(1).ToList();
File.WriteAllLines("file.txt",lines);
It will write all the lines to your file except the first line and replace your file content.So basically it will remove the first line from your file. 它将把除第一行以外的所有行都写入文件,并替换文件内容。因此,基本上它将从文件中删除第一行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.