Asp.net生成静态网页的实现代码

本文介绍了一个用于批量替换HTML文件中指定内容的工具类,通过读取原始HTML文件并替换特定字符串来更新页面。
  1. public class HtmlWriteFile  
  2. {  
  3.     public HtmlWriteFile()  
  4.     {  
  5.   
  6.     }  
  7.   
  8.     public static bool createHtml(string[] strnewsHtml, string[] stroldHtml, string strModeFilePath, string strPath)  
  9.     {  
  10.         bool flag = false;  
  11.         StreamReader sr = null;  
  12.         StreamWriter sw = null;  
  13.         string filepath = HttpContext.Current.Server.MapPath(strModeFilePath);  
  14.         Encoding code = Encoding.GetEncoding("gb2312");  
  15.         string s = string.Empty;  
  16.         try  
  17.         {  
  18.             sr = new StreamReader(filepath, code);  
  19.             s = sr.ReadToEnd();  
  20.         }  
  21.         catch (Exception ex)  
  22.         {  
  23.             throw ex;  
  24.         }  
  25.         finally  
  26.         {  
  27.             sr.Close();  
  28.         }  
  29.         try  
  30.         {  
  31.             for (int i = 0; i < strnewsHtml.Length; i++)  
  32.             {  
  33.                 s = s.Replace(stroldHtml[i], strnewsHtml[i]);  
  34.             }  
  35.             sw = new StreamWriter(HttpContext.Current.Server.MapPath(strPath), false, code);  
  36.             sw.Write(s);  
  37.             flag = true;  
  38.         }  
  39.         catch (Exception ex)  
  40.         {  
  41.             flag = false;  
  42.             throw ex;  
  43.         }  
  44.         finally  
  45.         {  
  46.             sw.Flush();  
  47.             sw.Close();  
  48.         }  
  49.         return flag;  
  50.     }  
  51.   
  52.     public static bool UpdateHtmlPage(string[] strNewsHtml, string[] strStartHtml, string[] strEndHtml, string strHtml)  
  53.     {  
  54.         bool Flage = false;  
  55.         StreamReader ReaderFile = null;  
  56.         StreamWriter WrirteFile = null;  
  57.         string FilePath = HttpContext.Current.Server.MapPath(strHtml);  
  58.         Encoding Code = Encoding.GetEncoding("gb2312");  
  59.         string strFile = string.Empty;  
  60.         try  
  61.         {  
  62.             ReaderFile = new StreamReader(FilePath, Code);  
  63.             strFile = ReaderFile.ReadToEnd();  
  64.         }  
  65.         catch (Exception ex)  
  66.         {  
  67.             throw ex;  
  68.         }  
  69.         finally  
  70.         {  
  71.             ReaderFile.Close();  
  72.         }  
  73.         try  
  74.         {  
  75.             int intLengTh = strNewsHtml.Length;  
  76.             for (int i = 0; i < intLengTh; i++)  
  77.             {  
  78.                 int intStart = strFile.IndexOf(strStartHtml[i]) + strStartHtml[i].Length;  
  79.                 int intEnd = strFile.IndexOf(strEndHtml[i]);  
  80.                 string strOldHtml = strFile.Substring(intStart, intEnd - intStart);  
  81.                 strFile = strFile.Replace(strOldHtml, strNewsHtml[i]);  
  82.             }  
  83.             WrirteFile = new StreamWriter(FilePath, false, Code);  
  84.             WrirteFile.Write(strFile);  
  85.             Flage = true;  
  86.         }  
  87.         catch (Exception ex)  
  88.         {  
  89.             throw ex;  
  90.         }  
  91.         finally  
  92.         {  
  93.             WrirteFile.Flush();  
  94.             WrirteFile.Close();  
  95.         }  
  96.         return Flage;  
  97.     }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值