现在的位置: 首页 > 综合 > 正文

c# 修改文本文件内容后并保存(转自百度知道)

2013年12月11日 ⁄ 综合 ⁄ 共 547字 ⁄ 字号 评论关闭
   public void WriteContext(string Context,string path) 
        {
            StreamWriter sw = new StreamWriter(path);
            sw.Write(Context);
            sw.Close();
            sw.Dispose();
        }

        private string ReadContext(string path)
        {
            FileStream fs = new FileStream(path, FileMode.Open);
            StreamReader sr = new StreamReader(fs);
            string context = sr.ReadToEnd();
            fs.Close();
            sr.Close();
            sr.Dispose();
            fs.Dispose();
            return context;
        }
或:
//读取文本 
StreamReader sr = new StreamReader(文本文件的路径);
string str = sr.ReadToEnd();
sr.Close();
//替换文本
str = str.Replace("E103","E103**");
//更改保存文本
StreamWriter sw = new StreamWriter(文本文件的路径,false);
sw.WriteLine(str);
sw.Close(); 

抱歉!评论已关闭.