62,041
社区成员
发帖
与我相关
我的任务
分享
// 生成静态页
private void GenerateHTML(string titleid)
{
string Bbs_TitleBody = "Bbs_TitleBody_" + titleid.ToString();//缓存名,以分类Id为参数
Cache.Remove(Bbs_TitleBody);
DataTable dt = BbsCache.Bbs_GetTitleById(int.Parse(titleid));
// Response.Write(dt.Rows[0]["title"].ToString());
string fileyyyymmdd = dt.Rows[0]["fileyyyymmdd"].ToString();
string filename = fileyyyymmdd + titleid + ".html";
string caid = dt.Rows[0]["caid"].ToString();
string caname = ddlXL.SelectedItem.Text;
string title = dt.Rows[0]["title"].ToString();
string content = dt.Rows[0]["body"].ToString();
string time = DateTime.Parse(dt.Rows[0]["createdate"].ToString()).ToShortDateString();
string username = dt.Rows[0]["username"].ToString();
string sourceurl = "/forum/Show.aspx?TitleId=" + titleid + "&CaId=" + caid;
#region 生成静态页
//创建当前日期的文件夹开始
string dir = Server.MapPath("~/" + "ITNews/" + fileyyyymmdd);//用来生成文件夹
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
//创建当前日期的文件夹结束
try
{
StringBuilder strhtml = new StringBuilder();
//创建StreamReader对象
using (StreamReader sr = new StreamReader(Server.MapPath("template.htm"), System.Text.Encoding.Default))
{
String oneline;
//读取指定的HTML文件模板
while ((oneline = sr.ReadLine()) != null)
{
strhtml.Append(oneline);
}
sr.Close();
}
//这几个是用来替换的。
string FilePath = "";
strhtml = strhtml.Replace("$Title", title);
strhtml = strhtml.Replace("$NewsCaId", caid);
strhtml = strhtml.Replace("$NewsCaName", caname);
strhtml = strhtml.Replace("$Time", time);
strhtml = strhtml.Replace("$UserName", username);//这里需要循环输出
strhtml = strhtml.Replace("$Content", content);//这里需要循环输出
FilePath = Server.MapPath("~/") + "ITNews" + "//" + fileyyyymmdd + "//" + filename;
strhtml = strhtml.Replace("$SourceURL", sourceurl);
//创建文件信息对象--------------------------------------------
FileInfo finfo = new FileInfo(FilePath);
//以打开或者写入的形式创建文件流
using (FileStream fs = finfo.OpenWrite())
{
//根据上面创建的文件流创建写数据流
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
//把新的内容写到创建的HTML页面中
sw.WriteLine(strhtml);
sw.Flush();
sw.Close();
}
}
catch (Exception ex)
{
//输出异常信息
Response.Write(ex.ToString());
}
#endregion
}