日期:2014-05-18  浏览次数:20797 次

如何查找并修改txt文件中的某些数据。
我做了一个按钮,单击按钮streamwrite写入某txt文件(续写方式。每次点击将新的内容另起一行写在后面)。
我想用这个txt代替数据使用。

如此,我怎么才能查找并修改规定的一行呢?

------解决方案--------------------
string.Replace
------解决方案--------------------
用循环遍历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