修改文本文件中特定行的内容 [英] Modify the content of specific line in text file

查看:94
本文介绍了修改文本文件中特定行的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,目前正在研究项目,因此我需要打开一个现有的文本文件并编辑特定行(即32)的内容,但是我失败了!有帮助吗?

I'm new on c# and currently working on project so that I need to open an existing text file and editing the content of a specific line (i.e. 32) but I failed! Any help?

推荐答案

根据您的问题,您知道行号,因此可以执行以下操作:

Well, based on your question you know the line number, so do something like this:

var lines = File.ReadAllLines("path to file");
lines[31] = "some value";
File.WriteAllLines("path to file", lines);

第一行代码将文件的所有行放入数组中.第二行清楚地将您的已知行设置为某个值,第三行用新的行集覆盖文件.

the first line of code gets you all the lines of a file into an array. The second line clearly sets thy known line to some value, and the third line overwrites the file with the new set of lines.

这篇关于修改文本文件中特定行的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆