如何查找并修改txt文件中的某些数据。

H_olic 2012-04-26 10:42:43
我做了一个按钮,单击按钮streamwrite写入某txt文件(续写方式。每次点击将新的内容另起一行写在后面)。
我想用这个txt代替数据使用。

如此,我怎么才能查找并修改规定的一行呢?
...全文
159 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
蝶恋花雨 2012-04-26
  • 打赏
  • 举报
回复
用循环遍历txt内容然后修改
String ReadTxt,WriteTxt;
WriteTxt= String.Empty;
Using(StreamReader sr = new StreamReader(TxtFilePath))
{
ReadTxt = sr.ReadToEnd();
}
String[] FileRead = System.Text.RegularExpressions.Regex.Split(ReadTxt , "\r\n");
foreach(String ReadLine in FileRead)
{
WriteTxt += ReadLine.Substring(0,ReadLine.LastIndexOf(',')) + ",0\r\n",
}
Using(StreamWritter sw = new StreamWritter (WriteTxtFilePath))
{
sw.Write(WriteTxt);
}
参考http://hi.baidu.com/jiabiting/blog/item/d6ddd9f07cb8afcd7831aa54.html
threenewbee 2012-04-26
  • 打赏
  • 举报
回复
string.Replace

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧