.net把详细页及列表生成为静态页1
2010-01-27 18:00
using System;using System.Collections.Generic;
using System.Text;
using System.IO; //StreamReader的命名空间
using System.Data;
using ***_logic; //内部的项目空间,隐藏一下
namespace db
{
public class createdb
{
/// <summary>
/// /////生成详细页面
/// </summary>
/// <param name="strMFile">模板文件名</param>
/// <param name="strId">新闻ID号</param>
/// <param name="strTilte">新闻标题</param>
/// <param name="strContents">新闻内容</param>
/// <returns></returns>
public bool MakeHtmlFile(string strMFile, string strId, string strTilte, string strContents)
{
//创建HTML的编码
Encoding ed = Encoding.GetEncoding("gb2312");
//获取存放模板的地址--相对路径
string strPath = System.Web.HttpContext.Current.Server.MapPath("../Html/");
//读取模板文件
string strTemp = System.Web.HttpContext.Current.Server.MapPath("../Module/" + strMFile);
//读取对象
StreamReader sr = null;
//写入对象
StreamWriter sw = null;
string str = "";
//读取模板文件
sr = new StreamReader(strTemp, ed);
//读到最后一行,赋值给STR
str = sr.ReadToEnd();
//自动生成HTML文件名
//string htmlfilename = strPath + DateTime.Now.ToString("yyyyMMddHHmmss") + ".html";
string htmlfilename = strPath + strId + ".html";
//从文本框里得到的值替换模板里的值
str = str.Replace("newstitle", strTilte);
str = str.Replace("newsbody", strContents);
//开始写入创建的HTML文件
sw = new StreamWriter(htmlfilename, false, ed);
sw.Write(str);
sw.Flush();
sw.Close();
return true;
}