///
/// 事件:生成HTML按钮
///
///
///
protected void lbCreateHTML_Click(object sender, EventArgs e)
{
List MDnews = NewsInfoManager.GetAllNewsInfo();
foreach (NewsInfo item in MDnews)
{
Generator(item);
}
BindNews();
ltrMsg.Text = "HTML页面已全部生成";
}
///
/// 方法:根据HTML模版生成静态HTML文件
///
///
private void Generator(NewsInfo entity)
{
string id = NewsInfoManager.GetEntityByTitleAndTile(entity) + "";
//读模板文件
string path = Server.MapPath(@"~\Templates\article.htm");
string temp = File.ReadAllText(path, Encoding.Default);
//替换
temp = temp.Replace("#title#", entity.Title);
temp = temp.Replace("#time#", entity.AddTime.ToString("F"));
temp = temp.Replace("#content#", entity.Content);
temp = temp.Replace("#msg#", DateTime.Now.ToString() + Environment.NewLine + "作者:南方学院");
//写入html文件中
path = Server.MapPath(string.Format(@"~\Articles\{0}.html", id));
if (File.Exists(path)) //如果存在,删除
{
File.Delete(path);
}
File.WriteAllText(path, temp, Encoding.Default);
}
///
/// 事件:删除指定目录下的HTML文件
///
///
///
protected void lbDeleteHTML_Click(object sender, EventArgs e)
{
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Server.MapPath("~/Articles/"));
FileInfo[] files = di.GetFiles("*.html");
foreach (FileInfo item in files)
{
item.Delete();
}
BindNews();
ltrMsg.Text = "HTML已全部清除";
}