- public class HtmlWriteFile
- {
- public HtmlWriteFile()
- {
- }
- public static bool createHtml(string[] strnewsHtml, string[] stroldHtml, string strModeFilePath, string strPath)
- {
- bool flag = false;
- StreamReader sr = null;
- StreamWriter sw = null;
- string filepath = HttpContext.Current.Server.MapPath(strModeFilePath);
- Encoding code = Encoding.GetEncoding("gb2312");
- string s = string.Empty;
- try
- {
- sr = new StreamReader(filepath, code);
- s = sr.ReadToEnd();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- sr.Close();
- }
- try
- {
- for (int i = 0; i < strnewsHtml.Length; i++)
- {
- s = s.Replace(stroldHtml[i], strnewsHtml[i]);
- }
- sw = new StreamWriter(HttpContext.Current.Server.MapPath(strPath), false, code);
- sw.Write(s);
- flag = true;
- }
- catch (Exception ex)
- {
- flag = false;
- throw ex;
- }
- finally
- {
- sw.Flush();
- sw.Close();
- }
- return flag;
- }
- public static bool UpdateHtmlPage(string[] strNewsHtml, string[] strStartHtml, string[] strEndHtml, string strHtml)
- {
- bool Flage = false;
- StreamReader ReaderFile = null;
- StreamWriter WrirteFile = null;
- string FilePath = HttpContext.Current.Server.MapPath(strHtml);
- Encoding Code = Encoding.GetEncoding("gb2312");
- string strFile = string.Empty;
- try
- {
- ReaderFile = new StreamReader(FilePath, Code);
- strFile = ReaderFile.ReadToEnd();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- ReaderFile.Close();
- }
- try
- {
- int intLengTh = strNewsHtml.Length;
- for (int i = 0; i < intLengTh; i++)
- {
- int intStart = strFile.IndexOf(strStartHtml[i]) + strStartHtml[i].Length;
- int intEnd = strFile.IndexOf(strEndHtml[i]);
- string strOldHtml = strFile.Substring(intStart, intEnd - intStart);
- strFile = strFile.Replace(strOldHtml, strNewsHtml[i]);
- }
- WrirteFile = new StreamWriter(FilePath, false, Code);
- WrirteFile.Write(strFile);
- Flage = true;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- WrirteFile.Flush();
- WrirteFile.Close();
- }
- return Flage;
- }
Asp.net生成静态网页的实现代码
最新推荐文章于 2021-08-08 21:18:56 发布
本文介绍了一个用于批量替换HTML文件中指定内容的工具类,通过读取原始HTML文件并替换特定字符串来更新页面。
6985

被折叠的 条评论
为什么被折叠?



