使用 C# 编辑文本文件中的特定行
我目前正在尝试用 C# 编辑文本文件。该文本文件是以以前的形式创建的,由以下内容组成:
Date Of Birth = 01/01/1980
Age = 31
Total = 40985
required1 =
required2 =
required3 =
该文本文件只有 13 行长,基本上我想忽略前三行,然后编辑以下 10 行。我最初尝试使用以下代码,但明显的缺陷附加到文件中:
List<string> newlines = new List<string>();
newlines.Add(Convert.ToString(required1));
newlines.Add(Convert.ToString(required2));
newlines.Add(Convert.ToString(required3));
System.IO.File.AppendAllLines(filepath);
我正在考虑使用 Streamreader 读取所有行,但如何编辑第三行向前是一个谜,是的,我是新使用 c# 任何帮助非常感谢。
I'm currently trying to edit a text file in c#. This text file is created in a previous form and consists of the following :
Date Of Birth = 01/01/1980
Age = 31
Total = 40985
required1 =
required2 =
required3 =
This text file is only 13 lines long, basically i want to ignore the first three lines then edit the following 10 lines. I tried initially with the following code but the obvious flaw is appending to the file:
List<string> newlines = new List<string>();
newlines.Add(Convert.ToString(required1));
newlines.Add(Convert.ToString(required2));
newlines.Add(Convert.ToString(required3));
System.IO.File.AppendAllLines(filepath);
I'm thinking using streamreader reading all the lines but how to edit the 3rd line onwards is something of a mystery, yes I'm new using c# any help is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您的文件很小,您可以将其整个加载到内存中。然后使用它并保存它,完全覆盖整个文件:
Since your file is small, you can load it whole into memory. Then work with that and save it, completely overwriting the whole file: