asp.net 生成html静态页

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text;
using System.IO;

/// <summary>
/// DB 的摘要说明
/// </summary>
public class DB
{
 
public DB()
 {
  
//
  
// TODO: 在此处添加构造函数逻辑
  
//
 }

    
public static bool WriteFile(string news_title,string news_content,string news_id)
    {   

        
//申明字符编码,命名空间System.Text
        Encoding encoding = Encoding.GetEncoding("gb2312");

        
//读取模板文件
        string TemplatePath = HttpContext.Current.Server.MapPath("template/1.htm");

        
//StreamReader实现一个 TextReader,使其以一种特定的编码从字节流中读取字符,命名空间System.IO。
        StreamReader sr = null;

        
//StreamWriter实现一个 TextWriter,使其以一种特定的编码向流中写入字符,命名空间Syste.IO。
        StreamWriter sw = null;

        
string str = "";

        
try
        {
            sr 
= new StreamReader(TemplatePath, encoding);

            
//从流的当前位置到末尾读取流
            str = sr.ReadToEnd();

        }
        
catch (Exception e1)
        {
            HttpContext.Current.Response.Write(e1.Message);
            HttpContext.Current.Response.End();
        }
        
finally
        {  
            sr.Close();
            sr.Dispose();
        }

        
//生成路径
        string OutPutPath = HttpContext.Current.Server.MapPath("html/");

        
//生成的文件名
        string newName = DateTime.Now.ToString("yyyyMM_"+ news_id + ".html";

        
//替换内容
        str = str.Replace("{title}", news_title);
        str 
= str.Replace("{content}", news_content);

        
//写文件
        try
        {
            sw 
= new StreamWriter(OutPutPath + newName,false, encoding);
            sw.Write(str);

            
//清理当前编写器的所有缓存区,并将缓存数据写入基础流。
            sw.Flush();
        }
        
catch (Exception e2)
        {
            HttpContext.Current.Response.Write(e2.Message);
            HttpContext.Current.Response.End();
        }
        
finally
        {
            sw.Close();
            sw.Dispose();
        }

        
return true;

    }
}

posted on 2007-10-22 14:00  jueban's space  阅读(233)  评论(0)    收藏  举报

导航