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

/// <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已全部清除";
        }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.duanlonglong.com/qdjy/573.html