根据模板生成网页

using System.Text;
using System.IO;

 

 

            string mbPath =Server.MapPath("template.htm");
            Encoding code = Encoding.GetEncoding("gb2312");
            StreamReader sr = null;
            StreamWriter sw = null;
            string str = null;
 
            //读取
            try
            {
                sr = new StreamReader(mbPath, code);
                str = sr.ReadToEnd();
 
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                sr.Close();
            }
 
            //根据时间自动重命名,扩展名也可以自行修改
            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
            str = str.Replace("$title$", txtTitle.Text);//替换Title
            str = str.Replace("$content$", txtContent.Text);//替换content
 
            //生成文件
            try
            {
                sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
 
                sw.Write(str);
                sw.Flush();
 
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                sw.Close();
                Response.Write("<a href=htm/"+fileName+" target=_blank>"+fileName+"</a>已经生成!");
            }

posted @ 2008-10-14 17:16  昕友软件开发  阅读(347)  评论(0编辑  收藏  举报
欢迎访问我的开源项目:xyIM企业即时通讯