生成与清除指定目录下HTML文件的方法

/// <summary> /// 事件:生成HTML按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param&g...
/// <summary>
        /// 事件:生成HTML按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbCreateHTML_Click(object sender, EventArgs e)
        {
            List<NewsInfo> MDnews = NewsInfoManager.GetAllNewsInfo();
            foreach (NewsInfo item in MDnews)
            {
                Generator(item);
            }
            BindNews();
            ltrMsg.Text = "HTML页面已全部生成";
        }
 
        /// <summary>
        /// 方法:根据HTML模版生成静态HTML文件
        /// </summary>
        /// <param name="entity"></param>
        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);
        }
 
        /// <summary>
        /// 事件:删除指定目录下的HTML文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        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已全部清除";
        }

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
王凯
王凯

92 篇文章

作家榜 »

  1. admin 651 文章
  2. 粪斗 185 文章
  3. 王凯 92 文章
  4. 廖雪 78 文章
  5. 牟雪峰 12 文章
  6. 李沁雪 9 文章
  7. 全易 2 文章
  8. bngvitmrbj 0 文章