简体   繁体   English

修改文本文件中特定行的内容

[英]Modify the content of specific line in text file

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 (ie 32) but I failed! 我是c#的新手,目前正在处理项目,所以我需要打开现有的文本文件并编辑特定行的内容(即32),但我失败了! 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. 第二行清楚地将您的已知行设置为某个值,第三行使用新的行集覆盖该文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM