基本的操作就是 文件的读写 和字符串操作
去掉
读取
public string refile(string filename,int comMessId)
{
StreamReader SReader = new StreamReader(System.Web.HttpContext.Current.Server.MapPath(文件路径), Encoding.UTF8);
string str = SReader.ReadToEnd();// System.Text.Encoding.UTF8.GetString(b, 0, b.Length);
SReader.Close();
SReader.Dispose();
return str;
}
写入
StreamWriter SWriter = new StreamWriter(TextBox1.Text);//创建StreamWriter实例
SWriter.Write("老虎老鼠猫大象猴子");//写入数据
SWriter.Close();
删除特定字符串的话 可以用replace函数
//using System.IO;
private void button1_Click(object sender, EventArgs e)
{
string fn = "D:\\test.txt";
StreamReader sr = new StreamReader(fn, Encoding.UTF8);
string s = sr.ReadToEnd();
sr.Dispose();
StreamWriter sw = new StreamWriter(fn);
sw.WriteLine(s.Replace("长颈鹿", ""));
sw.Flush();
sw.Close();
MessageBox.Show("请检查文件!");
}
string path = @"文件路径";
StreamReader sr = new StreamReader(path);
sr.ReadLine().Replace("长颈鹿",""); //查找到长颈鹿就替换成空
string path = @"d:\test.txt";
string content = File.ReadAllText(path).Replace("长颈鹿", "");
File.WriteAllText(path, content,Encoding.UTF8);