替换文件中特定的string?

sunnypjd 2006-01-13 04:33:18
替换文件中特定的string?
怎么弄?
...全文
136 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hyj_828 2006-01-16
  • 打赏
  • 举报
回复
把修改后的串讀入一個stream然後寫啊.

sunnypjd 2006-01-16
  • 打赏
  • 举报
回复
如何回写在同一个文件中?
光亮1916 2006-01-13
  • 打赏
  • 举报
回复
string.replace(oldstr,newstr)
maip 2006-01-13
  • 打赏
  • 举报
回复
参考一下这个,我是用来生成html页面的
Encoding code = Encoding.GetEncoding("gb2312");
// 读取模板文件htmltemplate是文件的路径如:/template/model.htm";
string temp = HttpContext.Current.Server.MapPath(htmltemplate);
StreamReader sr=null;
StreamWriter sw=null;
string str="";
try
{
sr = new StreamReader(temp, code);
str = sr.ReadToEnd(); // 读取文件
}
catch(Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}
finally
{
sr.Close();
}

// 替换内容
// 这时,模板文件已经读入到名称为str的变量中了
//$author$是要替换的变量,author是替换成的变量
str = str.Replace("$author$",author);
//$author$是要替换的变量,author是替换成的变量
str = str.Replace("$connent$",connent);
//$connent$是要替换的变量,connent是替换成的变量

try
{
sw = new StreamWriter(保存路径 + 文件名 , false, code);
sw.Write(str);
sw.Flush();

}
catch(Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
sunnypjd 2006-01-13
  • 打赏
  • 举报
回复
如何读?
如何回写?
lovefootball 2006-01-13
  • 打赏
  • 举报
回复
先把文件读到一个string中
string.replace()

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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