public static void DelPartOfFile(string path, string context)
{
try
{
FileStream fs;
if (!File.Exists(path))
{
fs = File.Create(path);
}
else
{
fs = new FileStream(path, FileMode.Open);
}
StreamReader sr = new StreamReader(fs);
while (sr.Peek() >= 0)
{
//如果读到该行内容与context,则删除该行内容
if (sr.ReadLine() == context)
{
//怎么处理?
break;
}
}
fs.Close();
sr.Close();
}
catch (Exception err)
{
}
}
我想删除文件中的一行内容,比如说a.txt文件有三行,分别是
aa
bb
cc
我输入的字符是bb
那执行上面函数后,文件内容就是
aa
cc
不知道应该怎么处理?
你说的想在文件里面直接替换后保存,不行的。
对 你可以把文件内容 读出来,编辑(删除后) 再 写回去(覆盖源文件)