[C#] C#使用模板生成html页面 →→→→→进入此内容的聊天室

来自 , 2021-02-20, 写在 C#, 查看 101 次.
URL http://www.code666.cn/view/fcac695d
  1. //使用模板生成HTML页
  2.     public static bool WriteFile(string strText, string strContent, string strAuthor)
  3.     {
  4.         string path = HttpContext.Current.Server.MapPath("~/news/");
  5.         Encoding code = Encoding.GetEncoding("gb2312");
  6.         // 读取模板文件
  7.         string temp = HttpContext.Current.Server.MapPath("~/news/text.html");
  8.         StreamReader sr = null;
  9.         StreamWriter sw = null;
  10.         string str = "";
  11.         try
  12.         {
  13.             sr = new StreamReader(temp, code);
  14.             str = sr.ReadToEnd(); // 读取文件
  15.         }
  16.         catch (Exception exp)
  17.         {
  18.             HttpContext.Current.Response.Write(exp.Message);
  19.             HttpContext.Current.Response.End();
  20.             sr.Close();
  21.         }
  22.  
  23.  
  24.         string htmlfilename = DateTime.Now.ToString("yyyyMMddHHmmss") + ".html";
  25.         // 替换内容
  26.         // 这时,模板文件已经读入到名称为str的变量中了
  27.         str = str.Replace("ShowArticle", strText); //模板页中的ShowArticle
  28.         str = str.Replace("biaoti", strText);
  29.         str = str.Replace("content", strContent);
  30.         str = str.Replace("author", strAuthor);
  31.         // 写文件
  32.         try
  33.         {
  34.             sw = new StreamWriter(path + htmlfilename, false, code);
  35.             sw.Write(str);
  36.             sw.Flush();
  37.         }
  38.         catch (Exception ex)
  39.         {
  40.             HttpContext.Current.Response.Write(ex.Message);
  41.             HttpContext.Current.Response.End();
  42.         }
  43.         finally
  44.         {
  45.             sw.Close();
  46.         }
  47.         return true;
  48.     }
  49. //csharp/800

回复 "C#使用模板生成html页面"

这儿你可以回复上面这条便签

captcha