设为首页
加入收藏
站内地图
旧版入口
当前位置:首页 > 站长学院 > 网络编程 > ASP.NET

一个自动生成html的类

作者:佚名 出处:网络转载 时间:10-01 点击:

内容载入中...
 

可以自动读取模板文件后,生成   当前 年/月/日/yyMMddhhmmss.html

代码如下

C#

private bool  CreateHtml(string strText,string strContent,string strAuthor)
  {
   string yearString=DateTime.Now.Year.ToString();
   string monthString=DateTime.Now.Month.ToString();
   string dayString=DateTime.Now.Day.ToString();

   string fileName=yearString+monthString+dayString+DateTime.Now.Millisecond.ToString()+".html";
   Directory.CreateDirectory(Server.MapPath(""+yearString+"file://"+monthString+"//"+dayString+""));
 
   Encoding code=Encoding.GetEncoding("gb2312");
   string temp=Server.MapPath("text.html");
   StreamReader sr=null;
   StreamWriter sw=null;
   string str="";
   //读取模板
   try
   {
    sr=new StreamReader(temp,code);
    str=sr.ReadToEnd();
   }
   catch(Exception exp)
   {
    Response.Write(exp.Message);
    Response.End();
    sr.Close();
   }
   //替换
   str=str.Replace("ShowArticle",strText);
   str=str.Replace("biaoti",strText);
   str=str.Replace("content",strContent);
   str=str.Replace("author",strAuthor);
  
   try
   {

    sw=new StreamWriter(Server.MapPath(""+yearString+"file://"+monthString+"//"+dayString+"//"+fileName+""),false,code);
    sw.Write(str);
    sw.Flush();
   }
   catch(Exception exp)
   {
    HttpContext.Current.Response.Write(exp.Message);
    HttpContext.Current.Response.End();
   }
   finally
   {
    sw.Close();
   }
   return true;

  }

HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
 <head>
  <title>ShowArticle</title>

 </head>
 <body>
  biaoti
  <br>
  content<br>
  author
 </body>
</html>


收藏本文:
】【打印页面】【推荐给朋友】【关闭窗口

站长学院

推荐信息