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

来源:互联网 发布:帝国cms 分页代码 编辑:程序博客网 时间:2024/05/16 12:23
  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.     }  
0 0